【问题标题】:What is ideal way to use multi threading with Core Data?在 Core Data 中使用多线程的理想方式是什么?
【发布时间】:2016-04-29 08:59:35
【问题描述】:

如何将多线程与块以及NSOperation和NSOperationQueue一起使用,从而不存在并发问题?

【问题讨论】:

    标签: ios multithreading core-data nsoperation


    【解决方案1】:

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

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

    我强烈建议查看Core Data Programming Guide 和并发部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      • 2015-10-10
      • 1970-01-01
      • 2010-11-30
      • 2012-01-12
      • 2023-02-15
      • 1970-01-01
      相关资源
      最近更新 更多