Will NSUndoManager undo changes happening in the background?

感情迁移 提交于 2019-12-10 10:16:32

问题


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?


回答1:


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];


来源:https://stackoverflow.com/questions/13898472/will-nsundomanager-undo-changes-happening-in-the-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!