2012-02-26 89 views
-1

只是想知道是否有其他人遇到過這種情況。coredata無法在xcode 4.3上工作?

我得到了這段代碼,曾經在以前的xcode版本中工作輝煌。

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 

    if (persistentStoreCoordinator != nil) { 
     return persistentStoreCoordinator; 
    } 

    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"mydb.sqlite"]; 
    /* 
    Set up the store. 
    For the sake of illustration, provide a pre-populated default store. 
    */ 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    // If the expected store doesn't exist, copy the default store. 
    if (![fileManager fileExistsAtPath:storePath]) { 
     NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"mydb" ofType:@"sqlite"]; 
     if (defaultStorePath) { 
      [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL]; 
     } 
    } 

    NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; 

    [self addSkipBackupAttributeToItemAtURL:storeUrl]; 

    NSError *error; 
    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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. 

     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. 
     */ 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    }  

    return persistentStoreCoordinator; 
} 

我期望從這一如下:

  1. 一個空的「mydb的」從頭開始創建,如果在我的包沒有「mydb.sqlite」。
  2. 如果一個「mydb.sqlite」存在於我的主包中,那麼我會期望它被複制到指定的diretory中。
  3. 如果「mydb.sqlite」與我的xcdatamodel不兼容,則應用程序必須崩潰。

但是,這隻適用於以前已經創建的數據庫。 例如,如果我嘗試把一個名爲「mydb.sqlite」的隨機數據庫放到我的包中,然後刪除原來的一個,那麼,

該應用程序不會崩潰!一個空白數據庫被創建並且新的數據庫被忽略。 這是完全錯誤的,因爲它違背了我的代碼。

此外,如果我加回原來的數據庫沒有任何反應,應用程序只是創建一個空白數據庫。

(是的,我做我的清理項目,刪除SIM卡的應用程序,並且發生任何改變之前甚至刪除build文件夾!)

任何想法?

+0

你的錯誤是什麼?你可以把它發佈出來嗎? – Raptor 2012-02-26 10:26:46

+0

沒有錯誤。這只是觀察不能按照我的代碼 – 2012-02-26 10:28:16

回答

0

問題/觀察的答案很簡單。

降級到4.2.1

我已經恢復了我的Xcode 4.2.1(感謝上帝的Time Machine),現在一切都如預期。

今天晚些時候我會向蘋果提交一個錯誤報告。

0

您的代碼與您的期望有所不同,正如您所說的那樣。

這是您的期望:

  1. 一個空的「mydb的」從頭開始建立,如果沒有「mydb.sqlite」在我的包。
  2. 如果在我的主包中存在「mydb.sqlite」,那麼我希望它被複制到指定的目錄中。
  3. 如果「mydb.sqlite」與我的xcdatamodel不兼容,則應用程序必須崩潰。

這裏是你的代碼做什麼:

  1. 尋找在Documents目錄
  2. 如果mydb.sqlite不存在mydb.sqlite,複製它從主束(如果它存在於主束)
  3. 創建一個新的持久性存儲在〜/文檔/ mydb.sqlite

我覺得您遇到的主要問題是由於第3步:addPersistentStoreWithType:...在提供的URL處創建新的商店。您需要重新查詢文件是否存在(以檢查是否存在可能已從MainBundle複製的文件),如果存在,則使用[persistentStoreCoordinator persistentStoreForURL:storeURL]而不是創建新的持久性存儲。

一旦你解決了這個問題,其他問題應該更容易追蹤。我建議在你的代碼中增加更多的NSLog,這樣你就可以看到代碼是否遵循你期望的執行路徑。例如。記錄您創建的每個新對象,以便您可以輕鬆查看它們中的任何一個是否爲nil。當你複製你的包數據庫時,添加一個NSError指針而不是NULL,然後如果錯誤不是零,請記錄它。

+0

應該是這樣的方式對不起。你說什麼,我期望什麼。我沒有在我的問題中說清楚。然而,這是DOESNT的工作。 – 2012-02-26 17:51:41

0

檢查您的默認數據庫實際上是否包含在軟件包中 - 即它被檢查爲包含在項目中,並處於複製文件構建階段。我重新安排了一個項目和一個SQLite文件,同時將其複製到項目結構中,但未包含在我的項目中 - 這會導致您看到的行爲。