2

NSUserDefaults is such an easy and convenient way to persist data on a device, and while it is encouraged as use for basic settings, I wonder, is there a practical limit to what you should use it for?

Suppose you have a large dictionary of many objects, 1000, and each of those objects is itself a dictionary with simple text strings as values. Overall, this big dictionary is probably not too large, since it only contains text, even if a fair amount of it.

Is it O.K. to use NSUserDefaults for something like this, your main data model?

johnbakers
  • 24,158
  • 24
  • 130
  • 258

1 Answers1

2

Why not just store that as an NSDictionary in a file?

See this post for a nice way to save your file as a plist: Save NSDictionary to plist

and getting it back is as easy as this:

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:fileName];
Community
  • 1
  • 1
Slee
  • 27,498
  • 52
  • 145
  • 243
  • I sorta simplified my question, but actually my main data model is a custom object that includes a couple dictionaries, some BOOL ivars, etc. It's not big, but I would think I could put this custom object itself inside the NSUserDefaults pretty easily. UPDATE: actually, I just read that NSUserDefaults only accepts standard plist objects. I wonder if that extends to the objects inside the plist objects? – johnbakers Aug 26 '11 at 00:20
  • @andrewx: "plist objects" is meant recursively-- everything in the data structure needs to be one of those types. You *can* encode other objects into there if you try hard enough, but I would not put your whole data model in NSUserDefaults. It's not what it's for, and if your app grows, you might need to migrate it out anyways. – Ben Zotto Aug 26 '11 at 00:22