ios - 无用的核心数据迁移错误 - 操作无法完成

标签 ios core-data core-data-migration

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

我有 3 个独立的实体需要迁移。每个上只有一个属性发生变化,并且它从整数变为字符串。一些实体似乎一切顺利,没有抛出异常,但该过程尚未完成。它列出了一堆本质上完全相同的错误:

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.)}

有什么解决此问题的最佳方法吗?如果有帮助,这是我正在使用的迁移策略:

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

最佳答案

好吧,我想这真的只是我误解了这个 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;
    }
}

关于ios - 无用的核心数据迁移错误 - 操作无法完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12923776/

相关文章:

iphone - Photobucket iOS 集成

ios - 从 coredata 返回 parent 和 child 不工作

ios - 核心数据 - 轻量级迁移和多个核心数据模型文件(xcdatamodel)

ios - 通过编程方式制作的 UINavigationController 不显示导航栏

ios - 如何创建具有对角线切割的 UIImageView?

ios - 在应用程序文本记录中持久(不在内存中)的最有效的方法/策略是什么?

ios - +[NSManagedObjectModel mergedModelFromBundles::forStoreMetadata:] 总是返回 nil

ios - 从应用商店更新后,应用程序因 SQLite 错误而崩溃

ios - 将新属性添加到现有实体时是否需要迁移核心数据?

ios - UITextField 与 UISearchBar?