在我的應用程序我已經使用核心數據自動輕量級移植罰款的多個版本。大約一年前,我實現了iOS文件共享,因此我將sqlite數據庫移到了/ Library文件夾而不是/ Documents目錄,以保持它隱藏。自那時起,我還沒有添加另一個核心數據版本。核心數據移動sqlite文件後自動輕量級遷移失敗
現在,當我嘗試添加另一個實體,增加模型版本我得到以下錯誤(截斷爲只顯示尾端):
"_NSAutoVacuumLevel" = 2;
}, reason=Can't find model for source store}
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason:
'This NSPersistentStoreCoordinator has no persistent stores.
It cannot perform a save operation.'
我驗證過.sqlite文件存在於如預期的那樣。我已經解決了幾天,似乎如果我將sqlite文件移回到/ Documents目錄,遷移工作正常。
下面是持久存儲協調代碼:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self libraryDocumentsDirectory] stringByAppendingPathComponent: @"myappdb.sqlite"]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:[self managedObjectModel]];
//Pass options in for lightweight migration....
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]) {
/*Error for store creation should be handled in here*/
NSLog(@"ERROR IN MIGRATION........%@", error);
}
return persistentStoreCoordinator;
}
- (NSString *)libraryDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
}
有沒有人遇到過這種情況之前?
在此先感謝您的幫助。
我的猜測是你的代碼正在尋找舊的目錄而不是新的目錄。一些細節將不勝感激。換句話說,你的代碼正在尋找Document dir。也許這就是問題所在。嘗試修改路徑。 –
顯示創建核心數據堆棧的代碼,以便我們可以診斷問題。 –