Reputation: 3189
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?
Upvotes: 2
Views: 519
Reputation: 46051
I recommend going the extra mile and use Core Data.
...don't be afraid, just try it. Start small and keep going.
Upvotes: 6
Reputation: 33406
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.
Upvotes: 1
Reputation: 49354
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.
Upvotes: 4