在两个不同上下文中的被管理对象之间建立关系
我正在解析 JSON 字符串,以在单独的线程和单独的托管对象上下文中创建新的托管对象。后来我想通过监听 NSManagedObjectContextObjectsDidChangeNotification 来合并主线程中的更改。
问题是我想在新解析的对象和主 moc 中的其他对象之间建立关系。但是我知道在不同上下文中的对象之间建立关系是非法的。
完成此任务的最佳实践是什么?
I am parsing JSON string to create new managed objects in a separate thread and in a separate managed object context. Later I want to merge the changes in the main thread by listening to NSManagedObjectContextObjectsDidChangeNotification.
The problem is that I want to establish the relationships between the newly parsed objects and the other objects in the main moc. However I know it is illegal to make relationships between objects in different contexts.
What's the best practice to accomplish this task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果主线程上的对象被保存,它们将可供辅助线程上的新上下文使用,因为新上下文共享对持久存储的访问。
如果您在两个线程上同时创建新对象,则需要将上下文相互合并,然后每个线程才能知道另一个线程上创建的对象。
合并本质上是在合并时生成彼此的上下文副本。
If the objects on the main thread are saved they will be available to the new context on a secondary thread because the new context shares access to the persistent store.
If you are creating new objects simultaneously on both threads, you will need to merge the context with each other before each will be aware of the objects created on the other.
Merging essentially makes the context copies of each other at the time of the merge.