2013-01-06 73 views
3

我想從模型中使用核心數據加載簡單的數據,並將其放入表視圖。這裏是我的持久性存儲下面的代碼:持久存儲的核心數據錯誤

//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數據庫預加載數據。一切都很完美。它能夠從模型中提取數據並正確顯示。

事情發生了變化,當我添加了第二個實體到該應用程序的最後部分相同的模型。該應用程序決定崩潰的「中止();」說以下內容:

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

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

  • 刪除app
  • 在iPhone模擬器
  • 重置內容和設置storeURL的名稱更改爲東西比其原始值不同等。
  • 卸下店網址如上
  • 代碼的註釋中列出清潔項目

我得出的結論是,那是因爲我已經把另一個實體,因爲當我刪除的實體,它完美運行並恢復正常,沒有任何問題。

我不知道發生了什麼,這是非常令人沮喪的,因爲我不知道如何解決這個問題。谷歌正在用盡這個問題的解決方案,所以我想我會得到驚人的堆棧溢出得到一個奇蹟的解決方案,以結束這種挫折。謝謝!

+0

@MartinR聽起來很容易,但我在哪裏添加註釋掉的代碼?我是否將[[NSFileManager ...]]與輕量級遷移的代碼一起包含在內? – Zack

+0

我刪除了我的評論,因爲flexaddicted已經在他的回答中解決了這個問題。 –

+0

這看起來像RHManagedObject。如果是,請參考它,因爲它不是你的代碼 – Rambatino

回答

8

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

直接從Core Data doc

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

如果您NSMigratePersistentStoresAutomaticallyOption設置YESNSInferMappingModelAutomaticallyOption你應該確定。這也被稱爲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如在下面的代碼段創建的。

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]) { 

} 
+0

非常感謝。但是我在if(![__ persistentStoreCoordinator addPersistentStore ...])中添加了這個嗎? – Zack

+0

@Zack看到我編輯的答案。 –

+0

@Zack我添加了一個編輯。可以嗎? –

相關問題