Undo Management with Core Data

I'm trying to implement undo support using Core Data on the iPhone and I ran into a few problems.

I currently have a couple of managed objects set up but when I make changes to their properties, these changes don't get recorded by the undo manager. From my understanding, Core Data is supposed to have this automatically set up and I should be able to have basic undo and redo support for changes, creation and deletion of managed objects.

Is there special way of making changes to the objects so that they get recorded by the undo manager? Or should I be registering undo actions for each change?

Also, suppose the application slides into a detailed view for editing a specific object. I would like to be able to undo all changes made when say, the cancel button is hit. Would undo grouping be applicable here? What is the difference between committing a group and have another undo manager manage the finer actions in the detailed view versus using just having one undo manager (the one included with the managed object context)?

Thanks!


ANSWERS:


While the undo features will work pretty much out of the box, you do need to allocate an NSUndoManager for the NSManagedObjectContext for which you want undo support.

The easiest way to do this is to set up the undo support when something asks your appDelegate for the NSManagedObjectContext

This is the default method that apple gives you:

- (NSManagedObjectContext *) managedObjectContext {

    if (managedObjectContext != nil) {
        return managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];

        [managedObjectContext setPersistentStoreCoordinator: coordinator];
    }
    return managedObjectContext;
}

Modify it to look like this:

- (NSManagedObjectContext *) managedObjectContext {

    if (managedObjectContext != nil) {
        return managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        managedObjectContext = [[NSManagedObjectContext alloc] init];

        //Undo Support
        NSUndoManager *anUndoManager = [[NSUndoManager  alloc] init];
        [managedObjectContext setUndoManager:anUndoManager];
        [anUndoManager release];

        [managedObjectContext setPersistentStoreCoordinator: coordinator];
    }
    return managedObjectContext;
}


 MORE:


 ? How I can set a date string from twitter to a NSDATE
 ? Cannot convert value of type 'Any' to specified type in swift
 ? Do properties of NSObjects inside an NSArray change or do they have to be added again to the NSArray?
 ? TableViewController Class didSelectRowAtIndexPath missing
 ? Error with Parse integration into Swift app
 ? How can I find out if I have Xcode commandline tools installed?
 ? iOS Firebase Crash Reporting Crashes on Set Up
 ? Xcode 8 Autocomplete takes 1 minute to appear
 ? Logging and breakpoints not working in XCTests
 ? How to show status bar in storyboard of Xcode