I have 3 MOC.
- MainThread MOC to display stuff (with undomanager)
- Background-save MOC to save data to disc (connected to store)
- Backgorund-update MOC to download data from server, parse it and save later
They are parent-child relation.
- Background-update -> 1. MainThread -> 2. Background-save (store)
Now when I download data from background I need to disable undomanager on mainthread so they are not undo - that can be a case that user is editing something at the same time.
Now question is if that is correct. I have that code in background-update thread
//create child background context which is child of 1. MainThread
NSManagedObjectContext* context = [[AppManager sharedAppManager] createChildManagedObjectContext];
//I'M DOING ALL CHANGES ON DATA HERE
[context.parentContext.undoManager disableUndoRegistration]; //disable undo on main thread
[context save:nil]; //save changes to background thread
[context.parentContext save:nil]; //save changes to main thread
[context.parentContext processPendingChanges]; //process changes on main thread
[context.parentContext.parentContext save:nil]; //save data to disc on 3. save-thread
[context.parentContext.undoManager enableUndoRegistration]; //enable undo again
With blocks it looks like that:
[context.parentContext performBlockAndWait:^{
[context.parentContext.undoManager disableUndoRegistration];
[context performBlockAndWait:^{
[context save:nil];
}];
[context.parentContext save:nil];
[context.parentContext processPendingChanges];
[context.parentContext performBlockAndWait:^{
[context.parentContext.parentContext save:nil];
}];
[context.parentContext.undoManager enableUndoRegistration];
}];
I'm asking because occasionally I'm getting some inconsistency crashes and I cannot really find a reason for those.