janineanne
janineanne

Reputation: 613

Core Data and undo groups

I'm working with Core Data for the first time and this has me stumped.

I have the following methods to handle grouping my changes and saving them:

- (void)beginUndoGrouping:(NSManagedObjectContext *)managedObjectContext {
    NSLog(@"begin");
    [managedObjectContext processPendingChanges];
    [managedObjectContext.undoManager beginUndoGrouping];
}

- (void)endUndoGroupingAndSaveContext:(NSManagedObjectContext *)managedObjectContext
{
    NSLog(@"end/save");
    [managedObjectContext processPendingChanges];
    [managedObjectContext.undoManager endUndoGrouping];
    [self saveContext:managedObjectContext];
}

- (void)cancelUndoGrouping:(NSManagedObjectContext *)managedObjectContext {
    NSLog(@"cancel");
    [managedObjectContext processPendingChanges];
    [managedObjectContext.undoManager endUndoGrouping];
    [managedObjectContext.undoManager undoNestedGroup];
}

Aided by the NSLog statements I know this is the sequence of events:

At this point my new Category is gone and I don't understand why. It was wrapped in a group, which was ended and saved. Shouldn't it be immune from being rolled back at that point? I would have expected the cancel to only affect any changes made in the item detail view. And if the way it's behaving now is correct, then how do I make it behave the way I was expecting?

Any clarification would be appreciated!

Upvotes: 2

Views: 939

Answers (1)

janineanne
janineanne

Reputation: 613

The answer turned out to be that you need to use a second managed object context for the inner group.

Upvotes: 2

Related Questions