I've found the same problem in a Core Data app where managed objects are changed by a background thread calling back to the main thread over several iterations of the event loop, and I need to cancel them as a group. Here is the trick I used...

NSUndoManager* mgr = [moc undoManager];

// Undo the cancelled operation.
[mgr undo];

// Now clear this incomplete operation from the redo stack by
// putting a dummy operation on the undo stack and then removing it.
[mgr registerUndoWithTarget:dummyRetainedObject
                selector:@selector(self) object:nil];

// The action must be removed on the next iteration of the event
// loop in order to make sure the undo group is closed.
[mgr performSelector:@selector(removeAllActionsWithTarget:) withObject:dummyRetainedObject afterDelay:0.];


On Jul 12, 2009, at 11:05 AM, Greg Titus wrote:


On Jul 12, 2009, at 7:10 AM, John Nairn wrote:

I added an option to cancel some action in the middle of the action. The most convenient coding was to implement the cancel by calling undo in the NSUndoManager, but this adds a "Redo" action to redo the partial or incomplete changes (and leaves the data in a poor state).

I can either re-code the change process or for an alternative, is there a way to perform to top level undo without having it moved to the redo stack? Or, similarly, is there a way to perform an undo and then remove the top most item in the redo stack?

First, I'd agree with I.S. that this sounds a little bit unclean, and that if there is a way to rearchitect this, I would.

Can't think of a cleaner way that wouldn't require and awful lot of caching changes before applying them. This would require a lot more code and potential for bugs when the undo mechanism does it perfectly.
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to