iPhone iOS如何合并Core Data NSManagedObjectContext? [英] iPhone iOS how to merge Core Data NSManagedObjectContext?
问题描述
我想在后台下载一些JSON对象,我做了相当多的多线程。一旦操作完成,我注意到这个断言失败:
I'm trying to download some JSON objects in the background and am doing quite a bit of multi threading. Once the operation completes, I noticed that this assertion fails:
NSAssert([user.managedObjectContext isEqual:[AppUser managedObjectContext]],@"Different contexts");
如何将更改合并到[AppUser managedObjectContext] strong>
How can I merge changes into the main context defined by [AppUser managedObjectContext] ?
推荐答案
在我的情况下,我使用RKObjectLoader的子类,有太多的线程和操作来跟踪的事情发生了。我发现RKObjectStore可以问我合并更改,通过要求加载对象保存自己。 (在我问 [AppUser managedObjectContext]
以保存自己,错误)之前
In my situation, I'm using a subclass of RKObjectLoader and there's way too many threads and operations to keep track of what's going on. I found that the RKObjectStore can be asked nicely to merge changes for me by asking the loaded object to save itself. (before I was asking [AppUser managedObjectContext]
to save itself, which was wrong)
正确的解决方案涉及请求加载对象将其自身保存在自己的上下文中,如下所示:
The correct solution involved asking the loaded object to save itself in its own context as follows:
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
AppUser* user = nil;
for (id object in objects)
{
user = object;
}
NSError* error = nil;
/**this call fires the:
// - (void)mergeChanges:(NSNotification *)notification
within the rkmanagedobjectstore class and merges changes made in this background operation over to the main contxt
*/
[[user managedObjectContext] save:nil];
}
这篇关于iPhone iOS如何合并Core Data NSManagedObjectContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!