gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:07:05 24 4
gpt4 key购买 nike

我有两个托管对象上下文

我从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 不会在其他数字或字符串数​​据中给出错误。

最佳答案

您不能在上下文之间传输托管对象,特别是当它们的上下文被初始化到其他持久存储时。您需要在新上下文中克隆托管对象,即创建具有与旧对象相同属性的新对象。

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

关于iphone - 非法尝试在不同上下文中的对象之间建立关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6100812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com