4

I've just got started with RubyMotion and, from what I can tell, it suggests using NSUserDefaults for a datastore. This seems kind of strange to me as the name suggests that it should be only used for storing config variables etc and not objects.

Is it okay to store my objects (such as User, Company, Tasks etc) in NSUserDefaults or should I use another approach?

Gerard
  • 4,818
  • 5
  • 51
  • 80

4 Answers4

6

User defaults are generally OK for storing small amounts of data that does not need encryption. The data which is large or could potentially grow over time should go either into the file system or into Core Data. Anything that needs to be secret (e.g. passwords) must go into keychain.

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

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3

NSUserDefaults should be used to save user preferences for the applications. It is not a general data storage. The NSUserDefaults class reference states:

The defaults system allows an application to customize its behavior to match a user’s preferences. For example, you can allow users to determine what units of measurement your application displays or how often documents are automatically saved.

So if your application manages users, companies and tasks then don't use NSUserDefaults for that.

Sebastian
  • 7,670
  • 5
  • 38
  • 50
1

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

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

If you want to store any other type of object, you should typically archive it to create an instance of NSData.

For more details, see NSUserDefaults Class Reference

Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
0

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

Question has nothing to do with RubyMotion and everything to do with Cocoa touch.

In general, user defaults are simply preference settings for a user, nothing more. Do not store data there that needs to be secured. Do not store data other than preference settings. Keep it small.

It's not an appropriate place to store other things like your objects.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55