Gytis
Gytis

Reputation: 670

Establish relationship between managed objects in two different contexts

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?

Upvotes: 1

Views: 358

Answers (1)

TechZen
TechZen

Reputation: 64428

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.

Upvotes: 1

Related Questions