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.
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:
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.
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 .
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.
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.
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 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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With