What is the proper way to be notified of a Core Data model object being restored after a deletion?

StackOverflow https://stackoverflow.com/questions/3586955

문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top