问题
I have two managedobjectcontext
I fetch object from managedobjectcontext1
and want two save it in the second object context while try to do that I get the error: "Illegal attempt to establish a relationship between objects in different contexts". The code I use
NSError * error;
NSPersistentStoreCoordinator *persistentStoreCoordinator_OLD;
NSPersistentStoreCoordinator *persistentStoreCoordinator_NEW;
NSManagedObjectModel *managedObjectModel_OLD;
NSManagedObjectModel *managedObjectModel_NEW;
NSManagedObjectContext *managedObjectContext_OLD;
NSManagedObjectContext *managedObjectContext_NEW;
NSURL *storeUrl_OLD = [NSURL fileURLWithPath: [[self applicationPrivateDocumentsDirectory] stringByAppendingPathComponent: @"CoreDataTutorialPart4.sqlite"]]; // CoreDataTutorialPart4.sqlite
NSURL *storeUrl_NEW = [NSURL fileURLWithPath: [[self applicationPrivateDocumentsDirectory] stringByAppendingPathComponent: @"Newpaxera.sqlite"]]; // CoreDataTutorialPart4.sqlite
// ADJUST THE MODEL
managedObjectModel_OLD = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
managedObjectModel_NEW = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
persistentStoreCoordinator_OLD = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel_OLD];
if (![persistentStoreCoordinator_OLD addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl_OLD options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
else {
NSLog(@"SUCCESS");
}
persistentStoreCoordinator_NEW = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel_NEW];
if (![persistentStoreCoordinator_NEW addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl_NEW options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
else {
NSLog(@"SUCCESS");
}
managedObjectContext_OLD = [[NSManagedObjectContext alloc] init];
[managedObjectContext_OLD setPersistentStoreCoordinator: persistentStoreCoordinator_OLD];
managedObjectContext_NEW = [[NSManagedObjectContext alloc] init];
[managedObjectContext_NEW setPersistentStoreCoordinator: persistentStoreCoordinator_NEW];
NSFetchRequest *fetchRequest_Study = [[NSFetchRequest alloc] init];
NSEntityDescription *entity_Study = [NSEntityDescription
entityForName:@"Studies" inManagedObjectContext:managedObjectContext_OLD];
[fetchRequest_Study setEntity:entity_Study];
NSMutableArray *StudiesList = [managedObjectContext_OLD executeFetchRequest:fetchRequest_Study error:&error];
for(int i =0 ; i < [StudiesList count] ; i++){
Studies *study = [StudiesList objectAtIndex: i ];
Studies *study_NEW = (Studies *)[NSEntityDescription insertNewObjectForEntityForName:@"Studies" inManagedObjectContext:managedObjectContext_NEW];
study_NEW.SudyID = study.SudyID;
study_NEW.StudyDate=study.StudyDate;
study_NEW.ModalityName=study.ModalityName;
study_NEW.Studiesstudent = study.Studiesstudent ; // raise sigapart error here
Studiesstudent
is an object of another entity class
Any suggestions for how to resolve this? Xcode does not give an error in the other numerical or string data.
回答1:
You cannot transfer managed objects between context, especially when they the context are initialized to other persistent stores. You need to clone the managed objects in the new context i.e. create new objects with the same attributes as the old.
Are you sure you don't need to migrate the persistent stores? It looks like you are trying to update an existing store to a new model. That is what migration is for.
来源:https://stackoverflow.com/questions/6100812/illegal-attempt-to-establish-a-relationship-between-objects-in-different-context