從兩個單獨的xcdatamodel文件定義的存儲遷移時,執行輕量級遷移時出現問題。核心數據 - 輕量級遷移和多核心數據模型文件(xcdatamodel)
在我的應用程序1.0版中,我將模型分解爲分析模型,模型-A和模型-B中的所有其他模型。編譯時,模型將被分組在一起,一切進展順利。
在新版本1.1上工作時,我通過將新型號版本添加到型號B並將該新版本設置爲活動狀態來升級型號B.
從1.0升級到1.1時出現此問題。看起來,Core Data檢查磁盤上的模型存儲(由版本1.0創建)並查找描述它的模型,但無法找到定義整個商店的SINGLE模型(模型-A僅涵蓋分析,模型-B覆蓋一切),所以它會拋出一個「無法找到源存儲模型」的錯誤。
有沒有人找到一種解決方案來分離模型,但仍然允許升級+輕量級遷移工作,而無需額外的定義自定義遷移的麻煩?
下面是代碼用來加載模型片段:
NSArray *modelNames = [NSArray arrayWithObjects:@"model-A", @"model-B", nil];
NSMutableArray *models = [NSMutableArray array];
for (NSString *name in modelNames)
{
LogInfo(@"loading model %@", name);
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:name withExtension:@"momd"];
NSManagedObjectModel *model = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] autorelease];
[models addObject:model];
}
// combine all the separate models into one big one
objectModel = [[NSManagedObjectModel modelByMergingModels:models] retain];
NSURL *documentsDirectory = [NSURL fileURLWithPath:[SuperFileManager documentsDirectory] isDirectory:YES];
NSURL *storeURL = [documentsDirectory URLByAppendingPathComponent:@"database.sqlite"];
NSError *error = nil;
coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:objectModel];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
nil];
if (![coordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:options
error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
這方面的更新? – codepushr
@codingrogue - 不幸的不是。我當時正在研究的團隊放棄使用Core Data,轉而使用Android編程。抱歉。 :/ – Mark