我有3个MOC。撤消管理器和多个MOC
- MainThread MOC显示的东西(带的UndoManager)
- 背景保存MOC来保存数据到光盘(连接到存储)
- Backgorund更新MOC从服务器下载数据,分析它并保存后
他们是亲子关系。
- 背景更新 - > 1. MainThread - >背景保存(存储)
现在,当我从后台我需要在mainthread禁用的UndoManager所以他们无法撤消下载数据 - 这可能是用户正在同时编辑某些内容的情况。
现在问题是如果这是正确的。我在后台更新线程代码
//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
积木,它看起来像:
[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];
}];
我这么问是因为偶尔我得到了一些不一致的崩溃,我真的不能找到一个理由对于那些。
嗨,谢谢你的提示。我正在使用performBlockAndWait,因为我在压力测试应用程序期间可以产生一些崩溃。在主线程中进行压力测试EDIT/SAVE时,在背景中进行更新。随着performBlock UndoManager进入一些不稳定的状态,并在该过程中崩溃。我会通读您的评论,并根据您的建议重新考虑我的解决方案 - 我可能会对此有所疑问,但需要先分析它:) –
如果'performBlockAndWait'可以防止崩溃......这意味着您在同步问题中其他可能在不阻碍操作的情况下解决的领域。 –
接受答案 - 花了我一段时间找到解决方案,但我最终扔掉undomanager和做的东西在单独的上下文,我扔掉了,如果用户取消。这解决了我的大部分问题,thx的想法。 –