使用Core Data的多线程的理想方法是什么?

时间:2016-04-29 08:59:35

标签: ios multithreading core-data nsoperation

如何使用块和NSOperation以及NSOperationQueue进行多线程处理,以便并发没有问题?

1 个答案:

答案 0 :(得分:0)

使用Core Data的正确方法是使用MainQueue上下文或PrivateQueue上下文。如果您在NSOperation中使用Core Data,则需要通过performBlock:performBlockAndWait:访问MainQueue上下文。如果您要使用PrivateQueue上下文,那么您仍然需要通过performBlock:performBlockAndWait:访问它,两者之间的主要区别在于针对MainQueue上下文所做的任何工作都将阻止用户界面。 / p>

理想情况下,如果您正在处理NSOperation中的数据,那么您应构建一个PrivateQueue上下文,该上下文是MainQueue上下文的子项,通过performBlock:performBlockAndWait:访问它并在您完成处理后保存它数据。

我强烈建议您查看Core Data Programming Guide和“并发”一节。