views:

27

answers:

2

I've got an elegant system set up using Core Data where any time a property of a model object is changed it is automatically reflected in its associated view using key-value observing but I have ran into a problem using undo.

The problem occurs when I have deleted a model object. The associated view is destroyed along with all the key-value observing when this occurs. The user suddenly decides that the deletion was a bad idea and issues an undo command restoring the model object. At this point the key-value observing has been destroyed and I can't seem to find a nice way to figure out which model object has been brought back from the dead and set everything up again.

The current solution I've thought of is registering for the NSUndoManagerDidUndoChangeNotification and then manually going through my Core Data model objects and seeing which ones do not have an associated view. I figure there must be a way to just know which particular object has been brought back though and thought this approach would be overkill.

I've also thought about creating an undo group where the removed view is re-added when the model object reappears but I would like to keep my undo manager related to the model only if that is possible.

I guess the solution I'm looking for is having the undo manager say, "Hey! Anybody who is interested listen up! I just did an undo and here is the Core Data model object that has been resurrected! Do with it what you will!" and then me setting up the view as if a new model object has been created.

Any ideas or guidance?

A: 

The problem occurs when I have deleted a model object. The associated view is destroyed along with all the key-value observing when this occurs.

I'm not sure what you mean by that but a view should not be so directly tied to the model that the view object itself dies when the model deletes something. The controller should be handling that and should be able to reverse it.

It sounds like you need to register the controller for one of the undo manager notifications. That will at least let you know when an undo has been performed and then you can take appropriate action.

TechZen
Sorry, it was pretty vague.I do have a controller in between the model and the view that handles all the key-value observing, deletion and other miscellany for setup and tear-down. When the controller observes that any property on the model object has changed it will update the view. When the controller is notified that the model object has been deleted it will remove the view and then mark itself for removal.I have looked at the undo manager notifications but it looks like I would have to look through all of my model to see which object has been restored. It'd work... but seems too heavy.
djdrzzy
+1  A: 

The following method of NSManagedObject could be the the right point to setup observings again:

- (void)awakeFromSnapshotEvents:(NSSnapshotEventType)flags

It is sent to a NSManagedObject after undo/redo operations

Martin Brugger
This works beautifully. I send out a notification once the undo insertion happens and then create the associated controller and view from the re-inserted model object. Thanks!
djdrzzy