0

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?

zoul
  • 102,279
  • 44
  • 260
  • 354
  • Is this graph of objects essentially the model for your application data? If so then you might want to look at the undo manager. If it has something to be undone then something in your graph will have changed. – aLevelOfIndirection Apr 18 '13 at 09:18
  • Yes, it’s a part of the model. I have never worked with the undo manager, I’ll take a look. Thanks! – zoul Apr 18 '13 at 09:36
  • If you have never used the undo manager then the above idea is unlikely to work. That's because you need to register reverse changes for all change to your model with the undo manager (how to undo). Since you are not already doing this well you can't really use the undo manager for this detection. – aLevelOfIndirection Apr 18 '13 at 10:24

0 Answers0