What are the limitations of NSUserDefaults?

Saving data permanently to iPhone is usually done using Core Data or sqlite3. Most users prefer to use NSUserDefaults as a repository for application preferences rather than using it as a regular database (e.g. sqlite).

I found that a large amount of data can be stored in NSUserDefaults, it is very easy to use and fast. So why not use this as persistent storage? What are the limitations of NSUserDefaults as a database?

Update:
I often use three different ways to save my data to disk.

  • Basic data
  • Serializing objects for plists
  • NSUserDefaults

I no longer use FMDB (or sqlite directly). What are the main advantages and disadvantages of each approach?

Some of the benefits NSUserDefaults I have come across:

  • Sorting, grouping, etc. can easily be done using NSPredicate.
  • NSUserDefaults is thread safe.
  • It takes one row to retrieve and save data in NSUserDefaults.
+44
objective-c iphone core-data nsuserdefaults
May 30 '11 at 7:52
source share
4 answers

NSUserDefaults offers a trivial learning curve and reliable threading.

Otherwise, I found Core Data excellent in every way. This is especially true for setting default values ​​and migration procedures.

Edit: As it turns out, NSUserDefaults "thread safety" seems to come from the current operations of the main thread. This led to a serious rollback in one of my applications; I ended up ripping off NSUserDefaults and replacing it with the thread-safe NSMutableDictionary, which serializes to a file.

+7
Nov 07 2018-11-11T00:
source share

Sqlite3 is more useful for storing a large database and accessing database items. You can sort the items in the Sqlite3 database, you can quickly find the item in the Sqlite3 dtabase. The Sqlite3 database has many privileges that NSUserDefaults did not have!




NSUserDefaults vs Sqlite3

NSUserDefaults is for custom settings, typically for basic objects such as NSString or NSNumber. Sqlite, serialization of a collection of objects in the property list, or Core Data are all valid parameters for storing user data, such as model objects you created.

You will not see the difference in speed, but it is best to choose the right mechanism for what you are doing. If these are just settings, use NSUserDefaults, otherwise I would serialize your objects to plist. If you are new to Cocoa, I would first avoid Core Data and even Sqlite to give you the opportunity to learn the basics first.




NSUserDefaults or Sqlite

If you want to store a large amount of data with some relationships, go to Sqlite if you want to keep a lower value for NSUserDefaults. Sqlite takes up some memory, so use it, only you really need to save complex data.




Using NSUserDefaults to save a lot of game data

Usually NSUserDefaults is used to save game settings. To save game data, it is usually better to use SQLite or you can create NSDictionary objects and save to disk, here are a couple of messages that may help:

+16
May 30 '11 at 7:57 a.m.
source share

For the project I'm currently working on, I need to create a large database (about 400,000 records). If you use NSUserDefaults, you must add entries that can take up to several minutes (depending on the device and the way you import your data). If you use CoreData, you can simply copy the previously created database to the document directory of your application and use it right away.

This is why I rely on CoreData.

+4
May 30 '11 at 8:23
source share

One of the advantages of CoreData is that your object will be an NSManagedObject with properties. This means that when you get or set values, you will have autocomplete to help you with property names. It also makes the code more readable.

Meanwhile, with NSUserDefaults, you should always use key-value for access, using strings for the key.

i.e:.

myGlobalSettingsObject.lastLoginTime = @(now);

against.

[[NSUserDefaults standardUserDefaults] setValue:@(now) forKey:@"lastLoginTime"];

What to do if you accidentally make a reservation when installing the key somewhere? The compiler will not warn you. What if someone puts the wrong type somewhere? The compiler will not warn you.

eg:.

[[NSUserDefaults standardUserDefaults] setValue:@"now" forKey:@"lastLoginTiem"]; ^ ^ ^^^^

... will not cause a warning or error during assembly ... is dangerous!

Other advantages of using NSManagedObject are that it can have validation; it can provide non-empty values; it can have custom retrieval and customization methods that you can use to make cool stuff; it can handle automatic migration if you change something about how all values ​​are stored; and its data model becomes part of your repository, so you can easily track change history.

Meanwhile, NSUserDefaults is fast and dirty, and great for basic small applications, but it's very unusual. Great for a small application, but if you have a huge application, it becomes difficult to manage compared to using Core Data.

The only possible thing about NSUserDefaults is that if your application needs to remove the CoreData repository or you don’t want to be nervous when implementing Thread-safe CoreData, this reduces maintenance in this regard.

+1
May 05 '16 at 17:54
source share



All Articles