【发布时间】:2016-04-29 08:59:35
【问题描述】:
如何将多线程与块以及NSOperation和NSOperationQueue一起使用,从而不存在并发问题?
【问题讨论】:
标签: ios multithreading core-data nsoperation
如何将多线程与块以及NSOperation和NSOperationQueue一起使用,从而不存在并发问题?
【问题讨论】:
标签: ios multithreading core-data nsoperation
使用 Core Data 的正确方法是使用 MainQueue 上下文或 PrivateQueue 上下文。如果您在 NSOperation 中使用 Core Data,那么您需要通过 performBlock: 或 performBlockAndWait: 访问 MainQueue 上下文。如果您要使用 PrivateQueue 上下文,那么您仍然需要通过 performBlock: 或 performBlockAndWait: 访问它,两者之间的主要区别在于,针对 MainQueue 上下文所做的任何工作都会阻塞用户界面。
理想情况下,如果您在 NSOperation 中处理数据,那么您应该构造一个作为 MainQueue 上下文子级的 PrivateQueue 上下文,通过performBlock: 或performBlockAndWait: 访问它,并在处理完数据后保存它。
我强烈建议查看Core Data Programming Guide 和并发部分。
【讨论】: