开发者

NSUndoManager with Core Data - Redo not working

开发者 https://www.devze.com 2022-12-22 18:03 出处:网络
I have a Core Data document-based app which support undo/redo via the built-in NSUndoManager associated with the NSManagedObjectContext. I have a few actions set up which perform numerous tasks within

I have a Core Data document-based app which support undo/redo via the built-in NSUndoManager associated with the NSManagedObjectContext. I have a few actions set up which perform numerous tasks within Core Data, wrap all these tasks into an undo group via beginUndoGrouping/endUndoGrouping, and are processed by the NSUndoManager.

Undo works fine. I can perform several successive actions, and each then undo each one of them successively and my app's state is maintained correctly. However, the "Redo" menu item is never enabled. This means that the NSUndoManager is telling the menu that there are no items to redo.

I am wondering why the NSUndoManager is seemingly forgetting about items once they are undone, and not allowing redos to occur?

One thing I should mention is that I'm disabling undo registration after a document is opened/created. When I perform an action, I call enableUndoRegistration, beginUndoGrouping, perform the action, then call processPendingChanges, setActionName:, endUndoGrouping, and finally disableUndoRegistration. This makes sure that only specific actions are undoable, and any other data changes I make outside of these go开发者_如何学Go unnoticed to the NSUndoManager. This may be a part of the issue, but if so I'm wondering why it's affecting redo?

Thanks in advance.


Actually, as long as you call enableUndoRegistration before both -undo and -redo (and disableUndoRegistration after them), you can achieve what you were after.

I believe that you were only calling enable(/disable)UndoRegistration before and after the changes to your managed objects. This means that your 'undo' was not registered with the undo manager, and hence you could not 'redo' it.


I have fixed this issue by:

Keeping undo registration enabled all the time, except for the times when I explicitly do not want to record undos.

I have learned that:

If you enable undo registration just before performing changes that you want to record, and disable immediately after committing those changes, the NSUndoManager's redo stack never gets populated.

So, never call disableUndoRegistration.


I called disableUndoManager while keeping "Redo" enabled. To do this I subclassed NSUndoManager and included this method:

-(void) undo
{
  [[appDelegate managedObjectContext] processPendingChanges];
  [self enableUndoRegistration];
  [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate:[NSDate date]];
  [super undo];
  [[appDelegate managedObjectContext] processPendingChanges];
  [self disableUndoRegistration];
}

As stated by Stefanf in NSUndoManager, Core Data and selective undo/redo: "NSUndoManager waits for the next run loop cycle until it registers your changes"

0

精彩评论

暂无评论...
验证码 换一张
取 消