gpt4 book ai didi

ios - 多线程核心数据 - permanentStoreCoordinator 与 ParentContext

转载 作者:行者123 更新时间:2023-11-29 01:17:21 26 4
gpt4 key购买 nike

我正在尝试解决由于从多个线程访问相同的 NSManagedObjectContext 而引起的问题。我在 Apple docs 中找到了以下内容:

let moc = … //Our primary context on the main queue

let privateMOC = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
privateMOC.parentContext = moc

privateMOC.performBlock {
//...
}

这似乎是我所追求的。我还在 tutorial 中发现了一些非常相似的东西已针对 iOS 9 更新:

let privateContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
privateContext.persistentStoreCoordinator = coreDataStack.context.persistentStoreCoordinator

privateContext.performBlock { () -> Void in
//...
}

他们似乎都实现了相同的目标,但是 Apple 文档的版本使用 parentContext 而不是直接使用 persistantStoreCoordinator。这两种方法有什么区别?

最佳答案

使用parentContext。自 iOS 5 以来,这优于使用持久存储协调器的方法。

Prior to OS X v10.7 and iOS v5.0, the parent store is always a persistent store coordinator. In OS X v10.7 and later and iOS v5.0 and later, the parent store may be another managed object context. Ultimately the root of a context’s ancestry must be a persistent store coordinator. The coordinator provides the managed object model and dispatches requests to the various persistent stores containing the data.

在您的特定情况下:

If a context’s parent store is another managed object context, fetch and save operations are mediated by the parent context instead of a coordinator. This pattern has a number of usage scenarios, including:

  • Performing background operations on a second thread or queue.

来源:NSManagedObjectContext 类引用,“Parent Store”。

关于ios - 多线程核心数据 - permanentStoreCoordinator 与 ParentContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35004721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com