Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the limitations of NSUserDefaults?

Storing data permanently in an iPhone is usually done using Core Data or sqlite3. Most people prefer to user NSUserDefaults as a storage for application preferences, rather than using it as a normal database (such as sqlite).

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

Update:
I frequently use three different ways of saving my data to disk.

  • Core data
  • Serializing objects to plists
  • NSUserDefaults

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

Some advantages of NSUserDefaults that I've come across:

  • Sorting, grouping, etc. can easily be done using NSPredicate.
  • NSUserDefaults is thread safe.
  • It takes one line to fetch and save data to NSUserDefaults.
like image 785
SEG Avatar asked May 30 '11 07:05

SEG


People also ask

How much data can you store in NSUserDefaults?

It appears the limit is the maximum file size for iOS (logically), which is currently 4GB: https://discussions.apple.com/thread/1763096?tstart=0. The precise size of the data is circumscribed by the compiler types (NSData, NSString, etc.) or the files in your asset bundle.

Is NSUserDefaults encrypted?

NSUserDefaults is easy to incorporate into your application and unfortunately, that means it is frequently misused by developers of all skill levels. Because NSUserDefaults stores all data in an unencrypted .

What types can you store natively in NSUserDefaults?

Storing Default Objects The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Boolean values, and URLs. These methods are described in Setting Default Values.

What is NSUserDefaults?

A property list, or NSUserDefaults can store any type of object that can be converted to an NSData object. It would require any custom class to implement that capability, but if it does, that can be stored as an NSData. These are the only types that can be stored directly.


2 Answers

Sqlite3 is more useful for keeping large database and to access to the database elements. You can sort the items of Sqlite3 database, you can search very fast for item in Sqlite3 dtabase. Sqlite3 database has many privileges that NSUserDefaults didn't have !


NSUserDefaults vs Sqlite3

NSUserDefaults is for user preferences, usually basic objects like NSString or NSNumber. Sqlite, serializing a collection of objects in a property list, or Core Data are all valid options for storing user data such as model objects you created.

You're not going to see a speed difference, but it's still best to pick the correct mechanism for what you're doing. If it's just preferences then use NSUserDefaults, otherwise I would serialize your objects to a plist. If you're new to Cocoa I would avoid Core Data and even Sqlite at first, to give yourself a chance to learn the basics first.


NSUserDefaults or Sqlite

When you want to store large amount of data with some relationship, go for Sqlite if you want to store less value go for NSUserDefaults. Sqlite occupies some memory so use it only you really need to save complex datas.


Using NSUserDefaults to save a lot of game data

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

  1. http://www.cocos2d-iphone.org/forum/topic/9308
  2. http://www.cocos2d-iphone.org/forum/topic/9210
like image 136
Viktor Apoyan Avatar answered Sep 30 '22 23:09

Viktor Apoyan


NSUserDefaults offers a trivial learning curve and thread safe implementation.

Otherwise I've found Core Data superior in every way. Especially with regards to configuring default values and migration routines.

Edit: As it turns out, NSUserDefaults "thread-safeness" seems to come from running operations on the main-thread. This caused severe frame-skipping in one of my applications; I ended up ripping out NSUserDefaults and replacing it with a thread-safe NSMutableDictionary which gets serialized to a file.

like image 43
lorean Avatar answered Sep 30 '22 23:09

lorean