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 829193
In Process

The Archive Base Latest Questions

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

Another Day, another CoreData problem,…but hopefully the last one now. Ok here is a

  • 0

Another Day, another CoreData problem,…but hopefully the last one now.

Ok here is a copy of what I have :

I have a List of Hotel Guests that stay in one Room and have Preferences. Once ready the user should select a guest and see the data and should also be able to add new guest, select the room (maintained also by application) and select their preferences (where the user can also add new preferences). The guest can have no or many preferences.

So here is what I have so far. I created 3 Entities : – Rooms with roomnumber – Preferences with name – GuestInfo with name -> with these Relationships room (Destination Rooms) and prefs (Destination Preferences with “To-Many Relationship”) The prefs is a NSSet when you create a Managed Object Class.

Now I created a UITableViewController to display all the data. I also have an edit and add mode. When I add a new Guest and just fill out the name, everything works fine. But when I want to add the prefs or the room number I get this error :

Illegal attempt to establish a relationship ‘room’ between objects in different contexts

Now, what confuses me is that when I add a guest and enter just the name, save it, go back and edit it and select the prefs and room number it works ?

I have this line in both ViewControllers to select the room or prefs :

[editedObject setValue:selectedRoom forKey:editedFieldKey];

with this .h :

NSManagedObject *editedObject;
NSString *editedFieldKey;
NSString *editedFieldName;

Again, it works on the editing mode but not when I want to add a fresh object.

And to be sure, this is what I do for adding an new Guest :

- (IBAction)addNewItem 
{

    AddViewController *addViewController = [[AddViewController alloc] initWithStyle:UITableViewStyleGrouped];
    addViewController.delegate = self;
    addViewController.context = _context;

    // Create a new managed object context for the new book -- set its persistent store coordinator to the same as that from the fetched results controller's context.
    NSManagedObjectContext *addingContext = [[NSManagedObjectContext alloc] init];
    self.addingManagedObjectContext = addingContext;
    [addingContext release];

    [addingManagedObjectContext setPersistentStoreCoordinator:[[_fetchedResultsController managedObjectContext] persistentStoreCoordinator]];

    GuestInfo *info = (GuestInfo *)[NSEntityDescription insertNewObjectForEntityForName:@"GuestInfo" inManagedObjectContext:addingContext];

    addViewController.info = info;

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addViewController];

    [self.navigationController presentModalViewController:navController animated:YES];

    [addViewController release];
    [navController release];
}

Anything I have to do to initialize the Room or Prefs ?
Hope someone can help me out. Thanks

  • 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-15T03:50:37+00:00Added an answer on May 15, 2026 at 3:50 am

    The problem you are experiencing is strictly related to your (wrong) use of NSManagedObjectContext objects. Basically you can not use two different managed object contexts to establish a relationship between an object managed by _context and another one managed by addingContext. Creating a new NSManagedObjectContext and passing it to a view controller to be pushed on the stack only works if you are going to add new objects to this contexts but not if you are going to establish relationships between objects belonging to different NSManagedObjectContext objects.

    This is why you get the message “Illegal attempt to establish a relationship ‘room’ between objects in different contexts”. To solve the problem, use your _context object to create and save the new objects.

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

Sidebar

Related Questions

Good day, I try to start new Activity from another. But it always crashed.
The page is here: https://gentle-day-3026.herokuapp.com/ The css file like this: https://gentle-day-3026.herokuapp.com/stylesheets/base.css another is to
another problem. I have already placed my Jython.jar into what my computer recognizes as
another day another problem, I finally managed to set up correctly google maps on
another day, another problem :) Since I woke up today I'm wondering about numbers.
another day, another problem. Honestly, it would be rather boring if this ever stopped,
Another day, another question... I have trolled the Intraweb for the answers and I
It's another day for being thick—so sorry. :) Anyhow, I have 2 arrays that
Good Day! i want to add a subview in another view in iphone. and
another day with Rails and today I want to use Ajax. linkt_remote_link for changing

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.