I do not have to save anything in NSUserDefaults

I just started working with RubyMotion, and as far as I can tell, it suggests using NSUserDefaults for data storage. This seems strange to me, because the name suggests that it should only be used to store configuration variables, etc., and not for objects.

Can I store my objects (for example, User, Company, Tasks, etc.) in NSUserDefaults or use a different approach?

+4
source share
4 answers

Custom defaults are generally suitable for storing small amounts of data that do not require encryption. Data that is large or can potentially grow over time should go either to the file system or to the underlying data. Everything that needs to be secret (e.g. passwords) must go into keychain .

Here is the answer with suggestions on how to choose among the storage models for your application .

+6
source

NSUserDefaults should be used to save user preferences for applications. This is not a shared data store. The reference to the NSUserDefaults class states:

The default system allows the application to customize its behavior according to user preferences. For example, you can allow users to determine which units are displayed in your application or how often documents are automatically saved.

So, if your application manages users, companies and tasks, then do not use NSUserDefaults for this.

+3
source

The NSUserDefaults class provides convenient methods for accessing common types such as floats , doubles , integers , Booleans and URLs .

The default object should be a list of properties, that is, an instance (or, for collections, a combination of instances): NSData , NSString , NSNumber , NSDate , NSArray or NSDictionary .

If you want to save any other type of object, you must archive it to create an instance of NSData .

For more information, see NSUserDefaults Class Help.

+1
source

See recommendations at https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/UserDefaults/AboutPreferenceDomains/AboutPreferenceDomains.html

The issue has nothing to do with RubyMotion and everything related to Cocoa touch.

In general, user defaults are simply user preferences, nothing more. Do not store data that you need to protect there. Do not save data other than preference settings. Keep it small.

This is not a good place to store other things, such as your items.

0
source

All Articles