【发布时间】:2010-11-29 23:37:36
【问题描述】:
我有 Core Data 应用程序,并且一直在迁移(升级)核心数据模型。每次我创建一个新版本时,我都会为每个版本创建一个映射模型。 现在我有 16 个版本,我有这样的映射模型: 1to2.xcmapingmodel 2to3.xcmapingmodel 3to4.xcmapingmodel ...ETC。最多 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]];
我在这里定义了自动迁移选项:
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]){
有人知道如何升级吗? 谢谢。
【问题讨论】:
标签: cocoa core-data mapping-model core-data-migration