1

私は手動移行に取り組んでおり、主にこのstackoverflowの回答をガイドとして使用しています: https ://stackoverflow.com/a/8155531/5416

移行する必要のある3つの個別のエンティティがあります。それぞれの1つのプロパティのみが変更され、整数から文字列に変更されます。一部のエンティティは正常に処理されているようで、例外はスローされていませんが、プロセスは完了していません。それはすべて本質的にまったく同じであるエラーの束をリストします:

Error Domain = NSCocoaErrorDomain Code = 1570 \ "操作を完了できませんでした\U2019。(Cocoaエラー1570)\" UserInfo = 0x2a4c2790 {NSValidationErrorObject = NSManagedObject_CCRecipeIngredient_2:、NSValidationErrorKey = name、NSLocalizedDescription=操作を完了できませんでした\U2019。(ココアエラー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 に答える