4

I've got an edit view controller which I'm using an NSUndoManager for which is the one set for my persistence store (core data project).

One of the features of my app is synchronisation with an external server. What I want to know is, if I am editing something in my view, and at the same time the app is syncing itself with the server, if I change my mind and decide to undo any changes in my current edit, would it also undo all of the changes made during the sync if they were made whilst the undo grouping had started, or would it only undo changes I'd made myself?

PaReeOhNos
  • 4,338
  • 3
  • 30
  • 41

1 Answers1

3

Depends on your implementation. Normally, the undo manager open an undo group for your event and encapsulates the changes, see groupsByEvent. If you use a secondary managed object context for your background synchronization and merge the context back in to the main context you have to ensure that you disabled the undo registration, see disableUndoRegistration.

Edit: Here is a little code snippet with that you can synchronize in a separate context without creating undo actions

// create a child context with no undo manager
NSManagedObjectContext *context = [NSManagedObjectContext contextWithParent:self.managedObjectContext];
context.undoManager = nil;

[... do your synchronization with the child context...]

// merge into main context without generating undo actions
[undoManager disableUndoRegistration];
[context save:&error];
[managedObjectContext processPendingChanges];
[undoManager enableUndoRegistration];

// to prevent undo action beyond the synchronization to remove all undo actions
[undoManager removeAllActions];
Stephan Michels
  • 952
  • 4
  • 18
  • Ah ok, so as long as my undo call is in a separate undo group to the sync stuff, then they'll be separate? – PaReeOhNos Jun 07 '13 at 21:59
  • Yes, but two questions arise. Should you be able to undo changes that comes with synchronization? And if not, can an undo action partially cancel a change of the synchronization. – Stephan Michels Jun 09 '13 at 15:06
  • No a synchronisation should not be able to be undone by the undo manager, and the undo action shouldn't be able to change anything from the synchronisation. If the user is editing a record that's actually being synchronised, and then they undo, it should simply go back to either how it was, or how it now is given that it's just been updated by the synchronisation – PaReeOhNos Jun 09 '13 at 17:25
  • If the user shouldn't be able to undo the changes of the synchronization, then you have to clear the undo stack after the synchronization. With that the user is only able to undo changes back to the latest synchronization. – Stephan Michels Jun 11 '13 at 09:54
  • But what happens if during the synchronisation it adds things to the undo stack? The sync will be updating lots of records not just one, so if it added a couple of changes, then the user starts editing and then undoes, wouldn't it then start undoing the sync changes? Is there a way of not adding the sync changes to the undo stack or doesn't that happen automatically anyway? I wasn't sure if it happened on its own if using the undomanage in the object context – PaReeOhNos Jun 12 '13 at 16:09