2

bit of a noob here but what is the best way to save simple variables/arrays for an app? For example I have an app that has a friendList array and I want that to load each time the app starts? Would it be NSKeyedArchiver? NSUserDefualts? Or some other way? Another example is say I have a simple int variable postCount to track the number of posts made, this needs to be saved on exit and loaded. What would be the best approach? Thanks!

Kex
  • 8,023
  • 9
  • 56
  • 129

3 Answers3

4

NSUserDefaults is best for small amount of data. If data is small the better option is NSUserDefaults else use NSKeyedArchiver.

In your case friendList array can contain huge amount of data, so I prefer NSKeyedArchiver for that. For Storing the postCount you can use NSUserDefaults.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
3

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. Archives and serializations are two ways in which you can create architecture-independent byte streams of hierarchical data. Byte streams can then be written to a file or transmitted to another process, perhaps over a network. When the byte stream is decoded, the hierarchy is regenerated. Archives provide a detailed record of a collection of interrelated objects and values. Serializations record only the simple hierarchy of property-list values.

You can store NSArray using NSUserDefault but better approach is You should use something like NSKeyedArchiver to serialize the array to an NSData, save it to the NSUserDefaults and then use NSKeyedUnarchiver to deserialize it later:

NSData *serialized = [NSKeyedArchiver archivedDataWithRootObject:myArray];
[[NSUserDefaults standardUserDefaults] setObject:serialized forKey:@"myKey"];

//...

NSData *serialized = [[NSUserDefaults standardUserDefaults] objectForKey:@"myKey"];
NSArray *myArray = [NSKeyedUnarchiver unarchiveObjectWithData:serialized];
nburk
  • 22,409
  • 18
  • 87
  • 132
user3575114
  • 993
  • 7
  • 13
  • 1
    okay thanks a lot, just wanted to check what the common programming practices where for storing simple program data like that. Thank you for your answer. Merry Christmas! – Kex Dec 25 '14 at 09:18
2

Small amounts of data related to the applications settings are easily kept in the UserDefaults. But don't go putting usernames and passwords in there - that's what the keychain is for.

If you just want to store arrays and dictionaries then serialising with NSKeyedArchiver is a reasonable solution.

However, and this is one of those things that is an engineering tradeoff, if you have large amounts of data, or data structures that are likely to change, then using a proper persistence mechanism is a better idea.

Proper persistence mechanisms such as CoreData, Realm.io, Mantle, give you far better control over the memory usage of your model objects, are better able to be scaled, and when your model objects change, then there is support for proper migrations.

It really depends on what you are saving, how it will be used, and how that use is likely to evolve.

Abizern
  • 146,289
  • 39
  • 203
  • 257