views:

3540

answers:

2

I'm working on a puzzle game, the game engine holds a complex hierarchy of objects, from game actors to vectors to transition animations. Depending on the game state, they might add up from 10 to 1000 (in extreme cases). I've been delaying the moment until I'd have to work on the saving and loading of all that mess, as I have no experience on this, but now the moment I was fearing is about to arrive.

Is it a good idea to use NSKeyedArchiver for this? Its concept seems powerful. That way I could rebuild the objects states, but does that include the pointers between saved objects? And how does it behave performance-wise? Will the system have time to save all these objects on disk on app termination?

+1  A: 

NSKeyedArchiver is quite powerful, but it will not let you retain linkages between objects (unless you add them yourself post-unarchiving). If you can/need to store your entire data set atomically, then NSKeyedArchiver is probably the way to go, but there will be some 're-hydration' involved on your part if there's anything more complex then parent-child relationships.

We have a complex hierarchy of data objects, we store them out to an SQLite database, and re-hook them up when we extract them.

Ben Gottlieb
thanks ben. I'm curious, what are the benefits of using sqlite on the iphone? Isn't it much work to translate from one to the other?And, are you working on games too? I'm curious of what is the common practice for games (or if there's a common practice).
Steph Thirion
It's not too much work to go between an SQLite row and an ObjC object. SQLite gives incremental storage (ie, you don't have to write out the entire data set each time), searching, sorting, etc. We use it in Crosswords, and our next, non-grame product (the one I referred to).
Ben Gottlieb
I only need to save 1 game state, if user quits and launches back. So I'd pretty much delete the whole database each time I'd save a new gamestate. In my case, is there any benefit I can take from sqlite?
Steph Thirion
You probably won't get much benefit from SQLite in this case; if the data structures are not too complex, you might just think about using a simple NSDictionary.
Ben Gottlieb
Ben, I don't think this is correct. The keyed archiver fully supports cycles, and any other types of graph relationships. You can even have weak references in the graph.
Jon Hess
I have to agree with Jon on this, NSKeyedArchiver/NSKeyedUnarchiver does maintain references.
mmoris
A: 

I went for NSKeyedArchiver, which does maintain references between objects. See here: http://stackoverflow.com/questions/1085316/easy-way-to-archive-interlinked-objects

Steph Thirion