Simple core data app doesn't support undo

Hello,

Amateur developer here.

I dusted off a hobby core data app coded mostly under OSX 10.7, and which supported undo "for free" (as Apple say it). It still works when I build it with recent Xcode versions, except that it no longer supports undo.

To understand why, I coded the most basic objective-C, core data (not document-based) app. In short, a single table view with a single column bound to an NSArray Controller set in "entity name" mode and bound to the managed object context. The core data model has a single entity class with a single NSString attribute (called "name", and which the table view column shows).

It all works well, I can add and remove entities, and change their names from the table. Changes are saved normally... except there is nothing I can undo. The undo menu item is greyed out.

I checked that my App Delegate implements the windowWillReturnUndoManager method and that this selector is called regularly.

So to check that the undo manager sees anything to undo, I added this method to my App Delegate:

- (IBAction)reportUndo:(id)sender {

    NSLog(@"undo: %d", [self.persistentContainer.viewContext.undoManager canUndo]);

}

I can call this method by pressing a button. But it always logs "undo: 0", even after I add or remove entities. I take that the undo manager sees nothing to undo.

This seems like rather basic stuff, but I can't understand why it doesn't work give given that it used to work in my more complex app without me doing anything special.

Accepted Reply

In case anyone is interested in the solution: I failed to do basic checks, i.e., that the managed object context I used had an undo manager that is not nil. In older macOS versions, a new context used to come with an undo manager by default. This must have changed with a macOS update.

Replies

I can't get a clear answer to this, but as online stuff describes how to implement undo in core data with code and does not appear to mention automatic undo, I'm coming to the conclusion that core data apps simply no longer support this ("automatic" as in "you don't have to code anything").

Can anyone confirm that I have to implement undo support with code? It would help if I got an answer to this instead or looking for how to make this work as it used to....

Thanks.

In case anyone is interested in the solution: I failed to do basic checks, i.e., that the managed object context I used had an undo manager that is not nil. In older macOS versions, a new context used to come with an undo manager by default. This must have changed with a macOS update.