开发者

Establish relationship between managed objects in two different contexts

开发者 https://www.devze.com 2023-04-08 02:41 出处:网络
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 NSManagedObje

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 objec开发者_StackOverflow中文版ts 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号