NSUndoManager, Master Data and Selective Undo / Redo

I am working on a basic data application that has a fairly large hierarchy of tree-like managed objects.

When a base object is created, it creates several child objects, which, in turn, create their own child objects, etc. Each of these children can collect information using NSURLConnections.

Now I would like to support undo / redo with undoManager in the managedObjectContext file. The problem is that if the user creates the base object, then he tries to cancel this action, the base object will not be deleted. Instead, one or more child objects may be deleted. Obviously, this type of action is unpredictable and undesirable.

So I tried to disable default deregistration. I did this by calling disableUndoRegistration: before anything was changed in the managedObjectContext file. Then, allowing you to unregister before basic operations, such as creating a base object, again turn off the registration of afterwords.

Now when I try to cancel, I get this error:

cancel: NSUndoManager 0x1026428b0 is in an invalid state, canceled caused by too many nested cancellation groups

Thoughts?

+7
source share
3 answers

NSUndoManager waits for the next cycle of the cycle of the cycle until it registers your changes.

 // do your stuff // give the run loop a breath [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate:[NSDate date]]; [undoManager disableUndoRegistration]; 
+6
source

More than a year since the publication of this question, but in any case, here is the answer:

You should check out the apple documentation that says:

.. A cancellation message closes the last cancellation group that was opened, and then applies all the cancellation operations in this group ... If any closed, nested cancellation groups are on the stack when the cancellation is called, this throws an exception. To cancel nested groups, you must explicitly close the group with the message endUndoGrouping, and then use undoNestedGroup to cancel it.

http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/UndoArchitecture/Articles/PerformingUndo.html

+5
source

My intersection with NSUndoManager is in invalid state, undo was called with too many nested undo groups not related to CoreData, but my answer may be useful nonetheless.

In my case, this manager exception was thrown due to an uncaught exception in my code that was raised during an NSUndoManager -undo call.

Looking back at the console, I could see both the exception of my application code and the NSInternalInconsistencyException cancellation manager.

I used the default undo group behavior for runloop and did not use explicit groups to unregister.

0
source

All Articles