1

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

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

错误域 = NSCocoaErrorDomain 代码 = 1570 \"操作无法\U2019t 完成。(Cocoa 错误 1570。)\" UserInfo=0x2a4c2790 {NSValidationErrorObject=NSManagedObject_CCRecipeIngredient_2:, NSValidationErrorKey=name, NSLocalizedDescription=操作无法\U2019t 完成。(可可错误 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;
    }
}
4

1 回答 1

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;
    }
}
于 2012-10-17T18:52:51.850 回答