核心数据手动迁移 [英] core data manual migration

查看:148
本文介绍了核心数据手动迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的项目中迁移到一个新的完全不同的模型。对于轻量级的迁移,这种改变太多了,我认为最好的方法是遍历顶级对象,并自己设置所有的属性和关系。

I'm trying to migrate to a new completely different model in my project. The changes are way too much for a lightweight migration and I think the best way is to iterate through the top level objects and set all the attributes and relationships myself.

如何我设置迁移过程是完全手动这样。我看过NSMigrationManager,似乎需要一个NSMappingModel。我看到的唯一的示例和教程使用 inferredMappingModelForSourceModel:destinationModel:error:,我不能使用,因为它不能推断映射模型。

How can I set up the migration process to be completely manual like this. I've looked into NSMigrationManager which seems to require an NSMappingModel. The only examples and tutorials I've seen use inferredMappingModelForSourceModel:destinationModel:error: which I can't use because it isn't able to infer the mapping model.

我在正确的路径上,如果是这样,我如何在代码中完全手动创建一个映射模型?感谢您的帮助。

Am I on the right path and if so how can I create a mapping model completely manually in code? Thanks for the help.

推荐答案

如果您的模型更改至少有一个源和目标实体级别映射示例您在旧模型中有一个 Vehicle 实体,现在要将该数据迁移到 Car ),那么您可以使用具有迁移策略的自定义映射模型。

If your model changes are such that you at least have an source and destination entity level mapping (For example you had a Vehicle entity in your old model and now you want to migrate that data to Car) then you can use a custom mapping model with a migration policy.

这个过程相当简单,在Xcode中,尝试向项目添加一个新的映射模型文件,选择源模型版本和目标模型版本。 Xcode试图聪明地找出源和目标实体的属性之间的映射。如果它不能,它只是离开的映射空白,你可以设置自己的映射。

The process is fairly straightforward, in Xcode, try to add a new mapping model file to your project, select the source model version and destination model version. Xcode tries to be smart about figuring out the mapping between attributes of your source and destination entities. If it isn't able to, it'll just leave the mappings blank and you can set your own mapping.

如果你想做一些除简单在映射期间为属性赋值或消隐或设置默认值,请使用称为 NSEntityMigrationPolicy 的东西。创建自己的子类并实现此方法以执行自定义映射:

If you'd like to do something other than simple assignment or blanking out or setting a default value for attributes during mapping, use something called an NSEntityMigrationPolicy. Create your own subclass and implement this method to do your custom mapping:

- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)instance
                                      entityMapping:(NSEntityMapping *)mapping
                                            manager:(NSMigrationManager *)manager
                                              error:(NSError **)error {
    NSArray *_properties = [mapping attributeMappings];
    for (NSPropertyMapping *_property in _properties) {
        if ([[_property name] isEqualToString:@"companyName"]) {
            NSExpression *_expression = [NSExpression expressionForConstantValue:@"10to1"];
            [_property setValueExpression:_expression];
        }
    }

    return [super createDestinationInstancesForSourceInstance:instance 
                                                entityMapping:mapping 
                                                      manager:manager 
                                                        error:error];
}

您可以详细了解如何进行自定义迁移此处

You can read more about how to do a custom migration here.

这篇关于核心数据手动迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆