2012-10-16 36 views
1

我正在手工遷移,主要使用這個計算器的答案作爲指導: https://stackoverflow.com/a/8155531/5416無益核心數據遷移錯誤 - 該操作無法完成

我有需要遷移的3個獨立的實體。每個屬性只有一個屬性發生變化,並且從一個整數變爲一個字符串。一些實體似乎經歷得很好,並沒有拋出任何例外,但這個過程並沒有完成。它列出了一堆是錯誤的所有的基本如出一轍:

錯誤域= NSCocoaErrorDomain代碼= 1570 \「操作 couldn \ U2019t完成(可可錯誤1570)\」的UserInfo = 0x2a4c2790 {NSValidationErrorObject = NSManagedObject_CCRecipeIngredient_2:, NSValidationErrorKey = name,NSLocalizedDescription =操作 無法完成。 (可可錯誤1570.)}

任何想法如何最好地解決這個問題?如果有幫助,下面是我使用的遷移策略:

- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)aSource 
             entityMapping:(NSEntityMapping *)mapping 
              manager:(NSMigrationManager *)migrationManager 
               error:(NSError **)error { 

    NSString *attributeName = @"foodId"; 

    NSEntityDescription *aSourceEntityDescription = [aSource entity]; 
    NSString *aSourceName = [aSourceEntityDescription valueForKey:@"name"]; 

    NSManagedObjectContext *destinationMOC = [migrationManager destinationContext]; 
    NSManagedObject *destEntity; 
    NSString *destEntityName = [mapping destinationEntityName]; 

    if ([aSourceName isEqualToString:@"CCFood"] || [aSourceName isEqualToString:@"CCFoodLogEntry"] || [aSourceName isEqualToString:@"CCRecipeIngredient"]) 
    { 
     destEntity = [NSEntityDescription 
          insertNewObjectForEntityForName:destEntityName 
          inManagedObjectContext:destinationMOC]; 

     // attribute foodid 
     NSNumber *sourceFoodID = [aSource valueForKey:attributeName]; 
     if (!sourceFoodID) 
     { 
      [destEntity setValue:@"0" forKey:attributeName]; 
     } 
     else 
     { 
      NSInteger sourceFoodIDInteger = [sourceFoodID intValue]; 
      NSString *sourceFoodIDString = [NSString stringWithFormat:@"%i", sourceFoodIDInteger]; 
      [destEntity setValue:sourceFoodIDString forKey:attributeName]; 

     } 

     [migrationManager associateSourceInstance:aSource 
          withDestinationInstance:destEntity 
           forEntityMapping:mapping]; 

     return YES; 
    } else 
    { 
     // don't remap any other entities 
     return NO; 
    } 
} 

回答

1

好了,我想這是真的只是一個誤會我這個API是如何工作的一個位情況。所有對象的屬性都不會自動映射。我在(錯誤的)假設下,我只需要設置我映射的屬性。最後,我所要做的就是迭代源實體的屬性並將它們分配給目標實體。

- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)aSource 
             entityMapping:(NSEntityMapping *)mapping 
              manager:(NSMigrationManager *)migrationManager 
               error:(NSError **)error { 

    NSString *attributeName = @"foodId"; 

    NSEntityDescription *aSourceEntityDescription = [aSource entity]; 
    NSString *aSourceName = [aSourceEntityDescription valueForKey:@"name"]; 

    NSManagedObjectContext *destinationMOC = [migrationManager destinationContext]; 
    NSManagedObject *destEntity; 
    NSString *destEntityName = [mapping destinationEntityName]; 

    if ([aSourceName isEqualToString:@"CCFood"] || [aSourceName isEqualToString:@"CCFoodLogEntry"] || [aSourceName isEqualToString:@"CCRecipeIngredient"]) 
    { 
     destEntity = [NSEntityDescription 
          insertNewObjectForEntityForName:destEntityName 
          inManagedObjectContext:destinationMOC]; 

     // migrate all attributes 
     NSEntityDescription *entity = [aSource entity]; 
     NSDictionary *attributes = [entity attributesByName]; 
     for (NSString *attribute in attributes) { 
      if ([attribute isEqualToString:@"foodId"]) { 
       // migrate the food id 
       NSNumber *sourceFoodID = [aSource valueForKey:attributeName]; 
       if (!sourceFoodID) 
       { 
        [destEntity setValue:@"0" forKey:attributeName]; 
        NSLog(@"migrating %@: empty foodid", aSourceName); 
       } 
       else 
       { 
        NSInteger sourceFoodIDInteger = [sourceFoodID intValue]; 
        NSString *sourceFoodIDString = [NSString stringWithFormat:@"%i", sourceFoodIDInteger]; 
        [destEntity setValue:sourceFoodIDString forKey:attributeName]; 
        NSLog(@"migrating %@ # %@", aSourceName, sourceFoodIDString); 

       } 
      } 
      else { 
       // not the foodid, so just pass it along 
       id value = [aSource valueForKey: attribute]; 
       [destEntity setValue:value forKey:attribute]; 
      } 
     } 

     [migrationManager associateSourceInstance:aSource 
          withDestinationInstance:destEntity 
           forEntityMapping:mapping]; 

     return YES; 
    } else 
    { 
     // don't remap any other entities 
     return NO; 
    } 
} 
+0

感謝您的回覆,這對我有很大幫助 – user2378197

相關問題