gpt4 book ai didi

iphone - NSPersistentStoreCoordinator 线程安全吗?

转载 作者:行者123 更新时间:2023-12-03 16:04:34 25 4
gpt4 key购买 nike

我正在开发一个使用 Core Data 的 iPhone 应用程序。该应用程序调用 Web 服务,解析生成的 XML 文件,然后在我的应用程序中创建或修改 Core Data 对象。我已经处理了 Web 服务调用并异步解析,但我一直将解析后的数据传回主线程来操作 Core Data 对象。我也想在后台线程中运行这个过程。 (1-2 秒的停顿并不能带来良好的用户体验)

显而易见的方法是专门为后台线程创建一个托管对象上下文,但后来我在 Apple's Core Data Programming Guide 中读到了这一行。 :

A persistent store coordinator provides to its managed object contexts the façade of one virtual store. For completely concurrent operations you need a different coordinator for each thread.

所以这里有一个问题:你不能有两个 NSPersistentStoreCoordinator 提供对同一存储的访问。但是,Marcus Zarra 的《Core Data》一书断言 NSPercientStoreCoordinator 是线程安全的,并且会序列化 I/O 请求(第 157 页)。

有人可以帮我解决这个问题吗?是否可以让第二个托管对象上下文在与主线程共享相同 NSPercientStoreCoordinator 的单独线程上运行?或者,更简洁地说,NSPersistentStoreCoordinator 是线程安全的吗?

最佳答案

Zarra 后来指出上下文会锁定存储,因此跨线程使用相同的 NSPercientStoreCoordinator 是可以的

Although the NSPersistentStoreCoordinator is not thread safe either, the NSManagedObjectContext knows how to lock it properly when in use. Therefore, we can attach as many NSManagedObjectContext objects to a single NSPersistentStoreCoordinator as we want without fear of collision.

关于iphone - NSPersistentStoreCoordinator 线程安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1976020/

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