簡體   English   中英

持久存儲的核心數據錯誤

[英]Core Data Error With Persistent Store

我正在嘗試使用Core Data從模型加載簡單數據並將其放入表視圖中。 以下是我的持久存儲的以下代碼:

//AppDelegate.m

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
 {
    if (__persistentStoreCoordinator != nil)
 {
    return __persistentStoreCoordinator;
}

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"vofasmmmnmgd.sqlite"];

if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]]) {
    NSURL *preloadURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"CoreDataSQLite" ofType:@"sqlite"]];
    NSError* err = nil;

    if (![[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURL error:&err]) {
        NSLog(@"Oops, could copy preloaded data");
    }
}

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

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

    /* Replace this implementation with code to handle the error appropriately.

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

     Typical reasons for an error here include:
     * The persistent store is not accessible;
     * The schema for the persistent store is incompatible with current managed object model.
     Check the error message to determine what the actual problem was.


     If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.

     If you encounter schema incompatibility errors during development, you can reduce their frequency by:
     * Simply deleting the existing store:
     [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

     * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
     [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

     Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
     */

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

  return __persistentStoreCoordinator;
}

正如您所看到的,我正在從sqlite數據庫中預加載數據。 一切都很完美。 它能夠從模型中提取數據並正確顯示。

當我將第二個實體添加到應用程序的最后部分的同一模型中時,情況發生了變化。 該應用程序決定崩潰“abort();” 說以下內容:

"The model used to open the store is incompatible with the one used to create the store";

現在這是一個簡單的解決方案,因為谷歌的研究已經發現了。 轉到應用程序並刪除它然后將重置所有數據庫的數據並讓它重新加載。 但是我已經這樣做了,但仍然無法正常工作。 以下是我嘗試的所有結果仍然相同:

  • 刪除應用
  • 重置iPhone模擬器上的內容和設置
  • 將storeURL的名稱更改為其原始值以外的其他名稱。
  • 刪除上面代碼注釋中列出的商店URL
  • 清潔項目

我得出的結論是,因為我添加了另一個實體,因為當我刪除它完全運行的實體時,它會恢復正常而沒有任何問題。

我不知道發生了什么事非常令人沮喪,因為我不知道如何解決這個問題。 谷歌已經沒有解決這個問題的解決方案,所以我想我會得到奇妙的Stack Overflow以獲得一個奇跡般的解決方案來結束這種挫敗感。 謝謝!

動機很簡單。 修改模型時,Core Data不知道如何映射它。

直接來自Core Data doc

因此,更改模型將使其與先前創建的商店不兼容(因此無法打開)。 如果更改模型,則需要將現有存儲中的數據更改為新版本 - 更改存儲格式稱為遷移。

如果為NSMigratePersistentStoresAutomaticallyOption設置YES ,對於NSInferMappingModelAutomaticallyOption ,則應該沒問題。 這也稱為LightWeight遷移。

NSMutableDictionary *options = [NSMutableDictionary dictionary];
[options setValue:[NSNumber numberWithBool:YES]
            forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setValue:[NSNumber numberWithBool:YES]
           forKey:NSInferMappingModelAutomaticallyOption];
NSPersistentStore *store = nil;
store = [coordinator addPersistentStoreWithType:NSSQLiteStoreType
                                        configuration:nil
                                                  URL:storeURL
                                              options:options
                                                error:&error];

編輯

只需將options字典傳遞給addPersistentStoreWithType:configuration:URL:options:error在以下代碼段中創建的addPersistentStoreWithType:configuration:URL:options:error

NSMutableDictionary *options = [NSMutableDictionary dictionary];
[options setValue:[NSNumber numberWithBool:YES]
           forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setValue:[NSNumber numberWithBool:YES]
           forKey:NSInferMappingModelAutomaticallyOption];
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM