非法尝试在不同上下文中建立对象之间的关系

时间:2011-05-23 17:39:44

标签: iphone objective-c ipad core-data

我有两个managedobjectcontext

我从managedobjectcontext1获取对象并希望将其保存在第二个对象上下文中,同时尝试这样做我得到错误:“非法尝试在不同上下文中建立对象之间的关系”。我使用的代码

    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是另一个实体类的对象

有关如何解决此问题的任何建议? Xcode不会在其他数字或字符串数​​据中给出错误。

1 个答案:

答案 0 :(得分:2)

您无法在上下文之间传输托管对象,尤其是在将上下文初始化为其他持久性存储时。您需要在新上下文中克隆托管对象,即创建具有与旧对象相同属性的新对象。

您确定不需要迁移持久性商店吗?您似乎正在尝试将现有商店更新为新模型。这就是迁移的目的。