【问题标题】:Unhelpful Core Data Migration error - The operation couldn't be completed无用的核心数据迁移错误 - 操作无法完成
【发布时间】:2012-10-07 02:07:45
【问题描述】:

我正在进行手动迁移,主要使用此 stackoverflow 答案作为指南: https://stackoverflow.com/a/8155531/5416

我有 3 个单独的实体需要迁移。每个属性只有一个在变化,它从整数变为字符串。一些实体似乎运行良好,没有抛出异常,但该过程没有完成。它列出了一堆本质上完全相同的错误:

Error Domain=NSCocoaErrorDomain Code=1570 \"操作 无法完成。 (可可错误 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;
    }
}

【问题讨论】:

    标签: ios core-data core-data-migration


    【解决方案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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-06
      • 2017-07-07
      • 2019-02-18
      • 2023-03-29
      • 2017-05-07
      • 2015-06-03
      • 2021-03-24
      相关资源
      最近更新 更多