Problem while adding new Object to CoreData App
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开发者_JAVA百科 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
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.
Don't create a new context. You must use the same context that created the room before. Why are you creating a new context every time you add a new item? That's why you get the error. Unless you have threading issues, you should create a single context when your application start or when you load your data, then use that throughout. Even with threading issues, you should generally only have one context per thread (or at least one context per temporary data set).
精彩评论