views:

201

answers:

3

In the development of my first serious iPhone application (Meaning I'd like to actually get it out on the AppStore), I needed a good way to represent my data. Instead of going with core data, I (stupidly, I think) decided to use classes to represent my data.

I created three classes, MMDot, MMShowMovement, and MMShow. The MMShowMovement holds instances of the MMDot class in an array, as the MMShow holds instances of MMShowMovement. Sounded like a nice way to do it, each class has a lot of logic that has to go with it, so I thought that classes seemed like a good way to go about representing all this data.

My question is, is there an easy way to save the instances of these classes so I can restore the data after the application is reopened? I have made applications on OS X using a NSKeyedArchiver, would it be similar on the iPhone? Would it be easier to start again using Core Data instead (These objects are very complex, especially MMDot with about 15 instance variables, now that I look at it, maybe not THAT complex)?

I'm really at a loss here.

+2  A: 

You can absolutely use NSKeyedArchiver to persist your objects. I'm a fan of either method, the decision mostly lies with your application needs. I don't think it will be much work to re-model your objects in CoreData. Without knowing anything about your application I'd say if you are doing simple persistence with a simple model you may not need Core Data, if you have complex relationships and will be reading/persisting data frequently during an application session Core Data is probably the way to go.

Greg Martin
Definitely agree with this assessment - keyed archiving and unarchiving is supported on the iPhone, so if you have relatively simple objects and are familiar with the architecture, go for it. On the other hand, Core Data *can* be very useful for object relationships - it also provides a bunch of other benefits (see http://stackoverflow.com/questions/1883879/why-should-i-use-core-data-for-my-iphone-app/1883957#1883957)
Tim
MMDot has about 15 attributes, most being NSIntegers and NSStrings, the MMShow and MMShowMovement classes have about three attributes each. MMShowMovement will hold somewhere between 40 and 100 instances of MMDot, and MMShow will hold about 4 instances of MMShowMovement.The only relation between classes is that MMDot has a "counts" attribute, and MMShowMovement will keep a NSInteger of the total amount of counts. I think I'll try using archiving. If it looks like it is going to be too slow. I'll shift over to Core Data.
Matt Egan
A: 

If you have a lot of object instances, you are probably better off using Core Data - it will help cache things, help you with queries to get specific sets of objects, and so on. KeyArchiving is really a lot more useful if you are saving off a handful of instances, but have some downsides - in particular it's harder to track down memory leaks from Unarchived objects.

Core Data can also do paging of results for you too, so that you aren't fetching in the entre contents of a large array if you do not need it.

Kendall Helmstetter Gelner
A: 

I was looking into serializing my classes the other day and went with using NSUserDefaults. I just give all the internals of my objects a unique key based on the object. Seemed to work fast for me.

Wrote a blog about it here: http://technolojiadev.blogspot.com/2009/12/serialize-this.html

Cairo