开发者

Replace main NSManagedObjectContext with the MOC from background thread rather than merge

When using a background thread to update Core Data, is there a way to replace the mainNSManagedObjectContext with the updated context开发者_如何学C from the background thread rather than callingmergeChangesFromContextDidSaveNotification?

I don't want to merge the two. I just want the updated context to replace the main context.


Depending on your needs, you could just save the context on the background thread, and then dispose of the context on the main thread and create a brand new one on the main thread.


You probably don't want to do this. Managed objects are associated with a MOC; you can't just change the "main" MOC and expect everything in your app to notice; you'll have to make all your objects that reference "main" MOs point to MOs from the new MOC.

-mergeChangesFromContextDidSaveNotification: (mostly) works fine for me.


The answer here was to switch back to the main context before the background context saves

[self performSelectorOnMainThread:@selector(deletemainmoc) withObject:nil waitUntilDone:YES];

and delete all the objects. then when it switches back to the background thread it saves and then you can merge into a clean context.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜