I have a graph of objects that support NSCoding
. And I have a separate class that takes care of the serialization itself, ie. it detects changes in the object graph and uses NSKeyedArchiver
to save it into a file.
I’m fairly happy with the design, but I’m not sure how to watch for changes in the object graph. One general solution I thought of was to add a version
property to each object that gets incremented with each change:
- (void) setFoo: (id) newFoo
{
_foo = newFoo;
self.version++;
}
When an object in the graph references other objects, it observes their version
(using KVO) and if it changes, the change is propagated upwards. The serialization class then observes the version
of the root node.
This works, but managing all the KVO stuff is cumbersome and prone to errors. Can you think of a better solution? One further constraint is that I would like the objects to know as little about the serialization as possible. They should know how to serialize (by implementing NSCoding
), but they shouldn’t care about much more.
This has to be a common problem in serialization frameworks, right?