Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6872513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:56:15+00:00 2026-05-27T03:56:15+00:00

Because my app is multi-threaded I use two NSManagedObjectContexts. The main context, that runs

  • 0

Because my app is multi-threaded I use two NSManagedObjectContexts. The main context, that runs in the main thread and another context that only runs in a separate thread.

I have created a small test app. It has two Core Data Entities. Parent and Child. Parent has a one-to-many relationship to Child. Child has a 1-1 relationship to Parent.

In one test method (that runs in a separate thread) I get a Parent instance, that has been created during a run time before. So it’s in the main context. I get this parent with this line of code:

Parent *tmpParent = [[parentController selectedObjects] objectAtIndex:0];

Then I create some children in the thread-context (managedObjectContextInBackground), set their parent to the tmpParent, give them a name and save the thread-context:

Child *child1 = (Child *)[NSEntityDescription insertNewObjectForEntityForName:@"Child" inManagedObjectContext:managedObjectContextInBackground];
[child1 setName:@"Homer"];
[child1 setParent:tmpParent];

Child *child2 = (Child *)[NSEntityDescription insertNewObjectForEntityForName:@"Child" inManagedObjectContext:managedObjectContextInBackground];
[child2 setName:@"Wilma"];
[child2 setParent:tmpParent];

[self saveManagedObjectContextInBackground];

If I execute that method the application crashes and says:
Illegal attempt to establish a relationship ‘parent’ between objects in different contexts

That’s why I added this line below the tmpParent declaration:

[managedObjectContextInBackground insertObject:tmpParent];

But, the application crashes again saying:
An NSManagedObject may only be in (or observed by) a single NSManagedObjectContext

I looked through the documentation, but I couldn’t find a way to solve this problem.

Question: How can I set the relationship of Child to Parent, when Parent is in a different NSManagedObjectContext, than Child ??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:56:16+00:00Added an answer on May 27, 2026 at 3:56 am

    Your object can be loaded from another context, thus, when you create a new NSManagedObject in your other thread, nothing stops you from fetching an existing NSManagedObject which may or may not be being used in the main thread, and use that reference in order to set up your relationship.

    When you exit the thread and merge the contexts, this change will get updated for the object that was until this point happily existing in memory on the main thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a multi-threaded app where main thread initiate two threads: MakeRequest Thread QueueListener Thread
I use two Spinner in my App. One of them is initially disabled because
I have a multi threaded app that writes log to file. occasionaly, saving fails
This question came about because the cells gem specifies template directories using File.join('app','cells'). That
I am writing an app that makes use of Object Output and Input Streams.
I have a Cocoa app with a custom, multi-step account setup that I implemented
I'm developing a multi device App with separate views for iPhone and iPad. Within
I have a simple multi-threaded .NET app which inserts rows into an MS Access
From the GAE getting started guide Because the App Engine datastore is not a
i am getting this error failed to execute request because the app-domain could not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.