views:

222

answers:

3

I was wondering what your thoughts were on persisting objects (NSDictionarys and NSMutableArrays of Objects) on the iPhone?

I've gone through a couple of options in my head:

1.) We persist using NSUserDefaults, this method seems the easiest and I sort of want to just go ahead and use this. The Data I want to store would only maybe be a few kilobytes of objects.

2.) Using NSKeyedArchiver (the exact name alludes me atm) to archive/serialize and then just puting that into a SQLITe database. but the problem with that is:

I need the data, when it's changed on the phone, to automatically be saved. NSUserDefaults has a synchronization method for this, but for method #2 I'd have to re-archive the entire array of objects, and then store it back into the database. This seems like a lot of work.

3.) forgot about this I've heard things about CoreData being able to handle this sort of thing, but I'm not sure if I understand exactly.

So what do you guys think? Do you have any other suggestions?

+4  A: 

I think NSDictionary and NSArray has writeToFile:atomically: instance methods that serializes and writes them to your given filename. You could add the observer for your NSDictionary or NSArray which writes them after they get changed.

Eimantas
+1  A: 

I've used both NSKeyedArchiver and writeToFile:atomically: on a few of my own apps with great success.

Performance has been great. You can repeatedly persist a few thousand lines of text and not notice.

Personally, I avoid sqlite unless you need querying. You'd be surprised what you can get away with using only NSDictionary filled with NSArray

The advantage to using NSKeyedArchiver is once you start creating "Domain Objects", you can persist those the same way. For this reason, I use NSKeyedArchiver 99% of the time.

bentford
+6  A: 

I recommend going the extra mile and use Core Data.

  • First you will learn a new thing, a good thing.
  • But further more your app will start up faster and handle data much more fluently as data is fetched in chunks rather requiring reading the whole dataset.

...don't be afraid, just try it. Start small and keep going.

epatel
I agree. Start with one of the Core Data templates for iPhone and play with it. You can even copy and paste code from the template directly into your project to get up and running quickly.
Marcus S. Zarra
I agree to, but: Core Data doesn't so arrays and dictionaries, so this means that your app will need to be designed around Core Data in order to take advantage of it. CD is not a bolt-on storage system like a file system. It's "core" to your app.
Steve Weller
Great answer, but you can't argue with the simplicity of NSKeyedArchiving a single domain object or NSDictionary.
bentford