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:
- start app in root view
- enter list view
begin - leave list view
end/save - enter item detail view
- enter category detail view
begin - touch Add Category button, which takes us to another view
begin - enter new data
- touch Done button
end/save - touch Back to go back to item detail view
- touch Cancel button
cancel - go back to list view
begin
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!