Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is NSUserDefaults used in iOS?

Tags:

I am new to iOS development and Mobile app development in general, I have decent knowledge about Objective C and MVC patterns to be used and I have never done any Mac development.

I am struggling to understand what NSUserDefaults is for?

We already have something like PList which stores data as XML and we have SQLite which is lightweight DB for such devices.

Then why do we have it?

Is it an alternative simple key-value storage for our app in the same way we have different types of storage on the cloud like an RDBMS and a key-value based NoSQL store etc ?

For NSUserDefaults, Apple docs say that :-

"Applications record such preferences by assigning values to a set of parameters in a user’s defaults database"

What do they mean by user's default database?

My guess is that, like in any multi-user operating system we have various user accounts and in the same way in Mac as well we might be having multiple users each having a database from where applications would load saved preferences for that user.

So like Mac OS X, does iOS also have multiple users and depending upon whichever is logged in NSUserDefaults picks his/her preferences?

Please tell me if I am wrong.

like image 295
Amogh Talpallikar Avatar asked Jan 10 '12 10:01

Amogh Talpallikar


People also ask

When should I use Core Data vs UserDefaults?

Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.

Is NSUserDefaults thread-safe?

The NSUserDefaults class is thread-safe.

Where are NSUserDefaults stored?

All the contents saved by NSUserDefaults is saved inside a plist file that can be found under Library -> Preferences -> $AppBundleId.

Is NSUserDefaults secure?

Because NSUserDefaults stores all data in an unencrypted . plist file, a curious person could potentially view this data with minimal effort. That means that you should never store any type of sensitive data inside NSUserDefaults.


2 Answers

NSUserDefaults are used to save small portions of data the App is dependent on to run correctly, while other approaches are good to store bigger amounts of data, that the app work with.

NSUserDefaults makes is incredible easy, as you don't have to worry about files, entities, fetches,…. It is all done in one line.

like image 45
vikingosegundo Avatar answered Oct 10 '22 04:10

vikingosegundo


One thing that hasn't been mentioned yet: NSUserDefaults supports all basic scalar (float, int, BOOL) types, as well as the plist-compatible types: NSData, NSString, NSNumber, NSDate, NSArray, and NSDictionary. (You mentioned plists as an alternative--NSUserDefaults is just a front-end to a standard plist file in the application's Preferences folder.) This makes it easy to create persistent storage for your application state in just a few lines of code. Even better, if you implement NSCoding on your model objects, you can archive them into an NSData and put them in NSUserDefaults:

NSData* data = [NSKeyedArchiver archivedDataWithRootObject:arrayOfModelObjects];  [[NSUserDefaults standardUserDefaults] setObject:data forKey:kDefaultsKeyModelObjects]; 

and restoring your app data is as simple as

- (void)viewDidLoad {     NSData* data = [[NSUserDefaults standardUserDefaults] objectForKey:kDefaultsKeyModelObjects];      arrayOfModelObjects = [[NSKeyedUnarchiver unarchiveRootObject:data] retain];      // error checking, tell UI to load the new values, etc... } 

(kDefaultsKeyModelObjects is a string constant you've defined somewhere. Always put your NSUserDefaults keys in one place! Having a typo in a key name can take hours to debug. :)

If you used, e.g., SQLite to store your application data, you have to write a lot of code to move your model data in and out of the database. That makes sense if you're dealing with a lot of data, need efficient searching, etc.; but if it's, say, a list of servers in a networking app, it's easier and cleaner to throw them in NSUserDefaults.

One last thing: NSUserDefaults is great for prototyping. Even if you know you're going to need a SQLite store eventually, you can start out using NSUserDefaults as a quick-and-easy persistent store, and get your UI working and your data model fleshed out before writing a bunch of database code.

So like Mac OS X, does iOS also have multiple users and depending upon whichever is logged in NSUserDefaults picks his/her preferences ?

Nope, on iOS the defaults plist is in the application's Library/Preferences folder, not in a user-specific folder like it is on OS X. I can't imagine iOS will ever have multiple logins, but you never know. If it did, they'd have to make separate defaults plists for different users.

like image 194
davehayden Avatar answered Oct 10 '22 03:10

davehayden