我想知道 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/