views:

214

answers:

2

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 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.

A: 

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.

CJ
+1  A: 

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.

Rob