objective-c - NSPrivateQueueConcurrencyType上下文只能在performBlock内操作?

标签 objective-c ios multithreading core-data concurrency

我想知道 NSPrivateQueueConcurrencyType 上下文是否仅在 -performBlock 和 -performBlockAndWait 内运行。 和 NSManagedObject 从 NSPrivateQueueConcurrencyType 上下文返回不能在执行 block 之外访问?

我从苹果文档“What's new in iOS 5.0”中找到了以下句子

When sending messages to a context created with a queue association, you must use the performBlock: or performBlockAndWait: method if your code is not already executing on that queue (for the main queue type) or within the scope of a performBlock... invocation (for the private queue type). Within the blocks passed to those methods, you can use the methods of NSManagedObjectContext freely.

如果是这样,为什么MagicalRecord和XMPPFramework不操作 block 内的上下文和NSManagedObject?

最佳答案

我不知道 MagicalRecord 和 XMPPFramework 中的代码做什么或不做什么。您需要通过他们的正常支持方式与他们联系。

但是,我可以告诉您,如果您使用 NSPrivateQueueConcurrencyType 创建 MOC,那么您访问该 MOC 或该 MOC 拥有的托管对象的唯一方法是通过 PerformBlock* API。

如果您使用 NSMainQueueConcurrencyType 创建 MOC,那么您可以在主线程中运行或通过 PerformBlock* API 访问它。

如果您使用 NSConfinementConcurrencyType 创建 MOC,则您只能从创建 MOC 的线程访问 MOC。如果未指定并发,则默认使用 Confinement。

这些是当前创建 MOC 的唯一方法,也是其使用规则的摘要。

我相信 MagicalRecord 使用限制并为每个线程保留一个 MOC,但我从未使用过它,并且只查看了旧版本的代码库,因此随着嵌套上下文的出现,它可能会发生很大的变化。

关于objective-c - NSPrivateQueueConcurrencyType上下文只能在performBlock内操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11436358/

相关文章:

ios - 核心数据 : Transformable Type in Entity | Fetch Failed

iOS/WatchKit : Is there a way for the watch kit app(swift) to use the iOS code (Objective C)?

ios - 一个接一个发送一堆请求

ios - 按钮在自定义标注中不起作用

ios - 在 obj C 中对 NSMutableArray 进行排序的最简单方法是什么

iphone - UITabBarController 的编程创建和配置 - 为选项卡设置系统图像

java - Android TCP/IP 和 GUI 到其他线程的通信 - Tic Tac Toe 多人游戏

java - 让线程暂停 - Thread.wait()/Thread.notify()

ios - NSLayoutConstrain - 将尝试通过破坏约束来恢复 - 以编程方式创建约束

ios - 如何在 iOS UISearchBar 中限制搜索(基于打字速度)?