I'm working on an app where user can edit some entries manually, but they can be updated from server as well.
For user updates I'm using UndoManager to allow user to cancel / confirm group of changes. When user enters edit mode I'm doing:
//when edit mode is entered
[[self.contact.managedObjectContext undoManager] beginUndoGrouping];
//when edit mode is finished
[[self.contact.managedObjectContext undoManager] endUndoGrouping];
//depending on button that was pressed to end edit mode, either:
[[self.contact.managedObjectContext undoManager] undo];
//or
[[self.contact managedObjectContext] save:nil];
For automatic updates from server (object can be changed by server) I'm using child-parent MOC. The one above which I'm using to manage edit mode is a parent one. I'm creating a child MOC and in background pull changes from server. At the end of process I'm doing:
[self.moc.save:nil]; //save to local MOC
[self.moc.parentContext.save:nil]; //save to main thread MOC (the one with undo)
My problem is that currently if changes are coming while user is editing and user decides to do UNDO - all changes that were automatically pulled are lost as well.
As a workaround I thought about:
- blocking auto-updates when user is in edit mode
- blocking edit mode when auto-update is in progress
But it sounds like a lot of work and potential bugs. Maybe there is a way to solve my problem on layer of MOCs, so user actions can be separated from automatic saves.