2010-11-17 33 views
3

我有很多條的品類很多關係的核心數據。試圖保存時,出現以下錯誤。我無法找到有關它的含義的信息,以及數據庫爲空時爲什麼有兩個版本。任何人都可以點亮一下嗎?iOS版:核心數據錯誤:NSMergeConflict爲NSManagedObject

錯誤:NSMergeConflict (0x76ae720) for NSManagedObject (0xd945560) with objectID '0xd943550 <x-coredata://09A438A8-E3F5-45FE-B9D7-106798E82E18/Article/p91>' with oldVersion = 1 and newVersion = 2

代碼:

NSMutableDictionary *dict = [[data objectAtIndex:i] valueForKey:@"category_names"]; 
     NSMutableArray  *values = [[NSMutableArray alloc] init]; 
     for (NSString *value in [dict allValues]) { 
      NSLog(@"value = %@", value); 
      [values addObject:value]; 
     } 

     NSMutableSet *setElements = [[NSMutableSet alloc] init]; 
     for (int i = 0; i < [values count]; i++) { 
      Category *cat = [self getCategoryFor:[values objectAtIndex:i]]; // Function which has fetch to get the category for the value "name" 
      [setElements addObject:cat]; 
     } 

     if ([setElements count] > 0) 
      [article addCategories:setElements]; 


     // Save the context. 
     NSError* error; 
     if (![managedObjectContext save:&error]) { 
      NSLog(@"Failed to save to data store: %@", [error localizedDescription]); 
      NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey]; 
      if(detailedErrors != nil && [detailedErrors count] > 0) { 
       for(NSError* detailedError in detailedErrors) { 
        NSLog(@" DetailedError: %@", [detailedError userInfo]); 
       } 
      } else 
       NSLog(@" %@", [error userInfo]); 
     } 

     [article release]; 
     [values release]; 
     [setElements release]; 

回答

4

你獲得不具有數據模型寫兩個不可調和的版本相同的持久性存儲實際上與數據本身,而是造成的錯誤文件。

您必須已經創建了一個數據模型,用它的一些數據寫入持久性存儲,然後更新模型。這通常不是問題,除非您如此修改數據模型以使自動遷移無法合併舊數據和新數據。

如果這是仍在開發中,你不需要現有的數據,最簡單的解決方法是刪除該應用程序關閉模擬器,然後建立與僅使用最新的數據模型,再運行它。這將不需要遷移,因此將跳過合併錯誤。

+0

謝謝你的幫助。我將數據添加到sqlite,然後將sqlite添加到項目中。這似乎會導致某種類型的版本問題。我加入的代碼所需要的所有數據,採取了DB了模擬器,它添加到項目中,問題就走開了。 – 2010-11-18 16:49:23

+0

這是非常困難的,直接添加數據到核心數據的SQL存儲,因爲它使用它自己的無證架構。 – TechZen 2010-11-18 18:10:07