0

Sorry if this has been already addressed (as always), but I couldn't find a clear answer so...

Circa OS X 10.7 (old stuff I know), a core data app that I was developing for the Mac had "automatic" undo support. This was a core data feature. Changes to the managed object context were simply undoable with the default "undo" menu without any custom line of code.

This no longer appear to work on this old Xcode project. The undo menu doesn't do anything and is greyed out (same app, same code).

To check, I created the most simple code data app projet (still Obj-C), and yep, it doesn't support automatic undo. The undo menu is always greyed out and when I send canUndo messages to the MOC's undo manager, it returns NO even after I make changes to managed objects.

Is automatic undo no longer a core data feature? Am I already too old? (You don't have to answer this one.)

Thanks.

jeanlain
  • 382
  • 1
  • 3
  • 13

2 Answers2

0

Most likely your managed object context's undoManager is nil, which is the default value. Core Data can't undo on its own but it can work with an undo manager. Either create one or pass in one that already exists-- for example, NSDocument has an undo manager, and your app delegate might have one.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • When I ask for the Window's undoManager, it is not nil. (I know about the undoManager as my code sends {setActionName} messages to it). The undoManager is the one provided by default in core data apps (in AppDelegate.m). – jeanlain Mar 08 '22 at 15:14
  • Thanks Tom! Indeed, the MOC of core data apps no longer has an undo manager by default, but I'm sure it used have before. I never explicitly added an undo manager to the MOC yet my app supported undo. I just added two lines of code at the right place and undo works again ! – jeanlain Mar 08 '22 at 15:52
0

That was a simple fix thanks to Tom's input and the answer to that issue.

macOS core data apps no longer have an undo manager set by default. They used to have one before (as indicated in the response to the issue linked above).

To add an undo manager to the managed object context, I just added these lines of code to the NSManagedObjectContext method of AppDelegate.m (just before the return line):

NSUndoManager *undoManager = [[NSUndoManager alloc] init];
[_managedObjectContext setUndoManager:undoManager];

and undo works again.

jeanlain
  • 382
  • 1
  • 3
  • 13