Ios 核心数据迁移:';Can';t合并具有两个不同实体的模型…';

Ios 核心数据迁移:';Can';t合并具有两个不同实体的模型…';,ios,core-data,migration,Ios,Core Data,Migration,我向核心数据模型添加了新版本。我向一个实体添加了新属性(Seriese) 但它引发了一个例外 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't merge models with two different entities named 'Seriese'' 我使用以下代码: - (NSPersistentStoreCoordinator *)persiste

我向核心数据模型添加了新版本。我向一个实体添加了新属性(
Seriese

但它引发了一个例外

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't merge models with two different entities named 'Seriese'' 
我使用以下代码:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationPrivateDocumentsDirectory] stringByAppendingPathComponent: @"CoreDataTutorialPart4.sqlite"]];

    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:  
                                            [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,  
                                            [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];


    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    return persistentStoreCoordinator;
}

有什么建议可以解决这个错误吗?我不想丢失保存的数据

问题很可能来自如何加载托管对象模型。默认方式是合并捆绑包中的模型,但是在这种情况下,您实际上有两个具有相同实体的模型(v1和v2)。。。这里很好地解释了这一点。…

核心数据不理解两个模型中的实体
Seriese
是同一实体,它应该将旧的
Seriese
的属性转换为新的
Seriese
。相反,它认为新的
系列
应该被视为一个全新的实体

这通常是由于尝试对自动迁移无法处理的新版本进行更改而导致的。自动迁移可以处理对属性名称的更改,添加属性或其他不会影响单个实体以外任何内容的更改。一旦开始更改关系和/或添加新实体,就必须进行手动迁移

你可以打电话

+[NSMappingModel inferredMappingModelForSourceModel:destinationModel:error:] 

。。。测试是否可以进行自动迁移。如果它返回nil和/或and error,则不能执行此操作

我阅读了链接,但在我的捆绑包中找不到momd,你能给我更多详细信息吗?在Xcode中,你是从工具栏中选择了“设计->数据模型->添加模型版本”还是只是编辑了现有模型?我做了设计->数据模型->添加模型版本Xcode有时会有点绑定。要做的第一件事是清除所有内容,然后手动删除构建目录,从设备/模拟器中删除应用程序并重新启动xcode。然后再试一次,如果不起作用,您可能需要完全删除模型并创建一个新模型。我总是通过创建一个版本化模型(即使我只想要一个版本)来启动项目(使用CoreData),这可以确保正确设置MOMD。另外,请检查模型目录,即中包含版本的文件夹,该文件夹已添加到您的目标。如果您仍然有问题,并希望向我发送项目的简单版本,我可以帮您查看。