非法尝试在不同上下文中建立对象之间的关系 [英] Illegal attempt to establish a relationship between objects in different contexts
问题描述
我有两个 managedobjectcontext
我从 managedobjectcontext1
,并希望两个保存在第二个对象上下文,同时尝试这样做,我得到的错误:非法尝试建立不同上下文中的对象之间的关系。我使用的代码
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
Studystudent
另一个实体类的对象
有关如何解决此问题的任何建议?
Any suggestions for how to resolve this? Xcode does not give an error in the other numerical or string data.
推荐答案
您不能在上下文之间传输托管对象,上下文被初始化为其他持久性存储。您需要在新上下文中克隆托管对象,即使用与旧对象相同的属性创建新对象。
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.
这篇关于非法尝试在不同上下文中建立对象之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!