1
votes

I'm working on a manual migration, primarily using this stackoverflow answer as a guide: https://stackoverflow.com/a/8155531/5416

I have 3 separate entities that need to be migrated. Only one property on each is changing, and it's changing from an integer to a string. Some of the entities seem to go through just fine, and no exceptions are being thrown, but the process is not completing. It lists a bunch of errors that are all essentially exactly the same:

Error Domain=NSCocoaErrorDomain Code=1570 \"The operation couldn\U2019t be completed. (Cocoa error 1570.)\" UserInfo=0x2a4c2790 {NSValidationErrorObject=NSManagedObject_CCRecipeIngredient_2:, NSValidationErrorKey=name, NSLocalizedDescription=The operation couldn\U2019t be completed. (Cocoa error 1570.)}

Any ideas how best to troubleshoot this? If it helps, here's the migration policy that I'm using:

- (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

1 Answers

1
votes

Ok, so I guess it was really just a case of me misunderstanding how this API works a bit. All of the object's attributes do not get mapped over automatically. I was under the (mistaken) assumption that I only had to set the attributes that I was mapping.

In the end, all I had to do was iterate over the source entity's attributes and assign them to the destination entity.

- (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;
    }
}