I'm making a drawing app. With each tap some core data entities are created. The user can hit the undo button and those clicks are undone one at a time. This works fine.
The problem comes when some automated task is run. I have a button that creates a bunch of core data entities (draws some stuff by itself) in a child context and then saves the context so changes get reflected in the main context.
The problem is it doesn't matter how many times the user hits the task button, all changes are grouped into one single undo step.
To make it clear: He makes 5 tasks and then hits undo once and he is back at the beginning. But I want him to be able to undo each task one at a time.
I'm using UIManagedDocument and it's Core Data stack. So it's 2 contexts, one that writes to disk, and a child of it for general use, and then I create a child of the latter for background operations.
This is my code:
NSManagedObjectContext* childContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
childContext.parentContext = self.document.managedObjectContext;
childContext.undoManager = self.document.undoManager;
[childContext performBlock:^{
//a lot of things happen here, that generate many entities
[childContext save:nil];
[self.document.managedObjectContext performBlock:^{
//completion stuff, update UI...
}];
}];