5

我有核心数据应用程序,并且一直在迁移(升级)核心数据模型。每次我创建一个新版本时,我都会为每个版本创建一个映射模型。现在我有 16 个版本,我有这样的映射模型:1to2.xcmapingmodel 2to3.xcmapingmodel 3to4.xcmapingmodel ...等。最多 16 个

这工作正常,但是当一个用户拥有版本 10 的数据文件并更新版本 16 的应用程序时会出现问题。我认为 Core Data 会自动从 10 升级到 16,但出现一个错误,显示“缺少映射模型”。为了确保映射模型正确,我将它一个一个升级到每个版本(10 到 11、11 到 12 等),它确实有效……这是我的代码。

我使用以下代码指定模型版本:

NSBundle *modelWrapper = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"TaskApp_DataModel" ofType:@"momd"]];
NSString *modelPath = [modelWrapper pathForResource:@"TaskApp_DataModel 16" ofType:@"mom"];
NSLog(@"%@",modelPath);
managedObjectModel = [[NSManagedObjectModel alloc]initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];

我在这里定义了 Migrate Automatic 选项:

  NSURL *url = [NSURL fileURLWithPath: [applicationSupportDirectory stringByAppendingPathComponent: @"storedata-sql"]];


NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];



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

有谁知道如何升级这个?谢谢你。

4

1 回答 1

1

它只会尝试从用户现有版本(可能是 v1)到当前版本。如果您有 3 个版本,那么您需要 v1-v2、v2-v3、v1-v3 的地图。16个版本都发货了吗?如果是这样,您可能需要开始制作新的迁移地图,如果您以前没有尝试过启用自动迁移,也可能值得启用,因为它可以很好地填补空白。我觉得是这样的:

[dict setObject:[NSNumber numberWithBool:YES] forKey:NSInfersMappingModel];

但你必须仔细检查。

很抱歉成为坏消息的带来者

于 2010-11-29T23:49:58.307 回答