Core Data - How to establish relationship between two objects in different contexts
In my app, I have the following CoreData model : a Foo has many Bar Entities : Foo <---->> Bar.
To add a new Foo entity, I create a new MOC in which I create a new instance of Foo. This displays the AddFooViewController. Here, I can either Cancel or Save or Add a new Bar entity for this created Foo entity. Problem happens when I add a new Bar entity.
Let me explain in detail : the new Bar entity is created in a totally new MOC. Then, when I decide to save the Bar entity, I try to merge both "Add Bar MOC" and "Add Foo MOC". This should be done by this code :
- (IBAction)addNewBar {
BarAddViewController *addViewController = [[BarAddViewController alloc] initWithStyle:UITableViewStyleGrouped];
NSManagedObjectContext *addingContext = [[NSManagedObjectContext alloc] init];
self.addingManagedObjectContext = addingContext;
[addingContext release];
[addingManagedObjectContext setPersistentStoreCoordinator:[self.managedObjectContext persistentStoreCoordinator]];
// Create new bar
Bar *bar = (Bar *)[NSEntityDescription insertNewObjectForEntityForName:@"Bar" inManagedObjectContext:self.addingManagedObjectContext];
bar.creationDate = [NSDate date];
addViewController.delegate = self;
addViewController.bar = bar;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addViewController];
[self.navigationController presentModalViewController:navController animated:YES];
[addViewController release];
[navController release];
}
// Delegate method :
- (void)barAddViewController:(BarAddViewController *)controller didFinishWithSave:(BOOL)save {
// Dismiss the modal view to return to the main list
[self dismissModalViewControllerAnimated:YES];
// Save modifications
if (save) {
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc addObserver:self
selector:@selector(addBarControllerContextDidSave:)
name:NSManagedObjectContextDidSaveNotification
object:addingManagedObjectContext];
// Assign relationship here ???? foo.bars = bar;
NSError *error;
if (![addingManagedObjectContext save:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
[dnc removeObserver:self
开发者_如何学JAVA name:NSManagedObjectContextDidSaveNotification
object:addingManagedObjectContext];
} else {
// Remove bar from addingManagedObjectContext
[self.addingManagedObjectContext deleteObject:controller.bar];
NSError *error;
if (![addingManagedObjectContext save:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
}
// Release the adding managed object context.
self.addingManagedObjectContext = nil;
}
- (void)addBarControllerContextDidSave:(NSNotification*)saveNotification {
[self.managedObjectContext mergeChangesFromContextDidSaveNotification:saveNotification];
}
My problem is that I don't know where to establish the relationship between the Foo and the Bar. I know I can only establish the relationship between two NSManagedObject that are in the same context. Does somebody could help me, please ?
Thanks in advance
You need to load both entities in the same managed context to create any sort of relationship (though I guess you've already figures that out!).
You have a Foo from the first managed object context - call it Foo1.
Foo *foo1 = {your initial Foo};
// Get a foo from the new managed context
NSManagedObjectID fooID = [foo1 managedObjectID];
Foo *foo2 = [addingManagedObjectContext objectWithID:fooID];
You can now use Foo2 to create the relationship
精彩评论