核心数据迁移:属性映射值表达式 [英] Core Data Migration: Attribute Mapping Value Expression

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

问题描述

我目前在我的实体上有一个cardType属性,在旧模型中可以是Math,Image或Text。在新模型中,我将只使用Math和Text并且还具有hasImage属性,如果旧的cardType是Image(我想要更改为Text),我想将其设置为true。

I currently have a cardType attribute on my entity, which in the old model could be "Math", "Image" or "Text". In the new model, I'll be using just "Math" and "Text" and also have a hasImage attribute, which I want to set to true if the old cardType was Image (which I want to change to "Text").

最后,我有一套另一个实体,卡片,其中一套可以与一副套牌相关联,在每一个中,我也会如果甲板之前是图像类型,我想要设置为true的hasImage。

Lastly, I have a set of another entity, "card", of which a set can be associated with a deck, and in each of those, I'll also have hasImage which I want to set to true if the deck was of "Image" type before.

这是否可以使用我在两个版本之间创建的映射模型中的值表达式,还是我必须做其他事情?

Is this all possible using the Value Expression in the Mapping Model I've created between the two versions, or will I have to do something else?

我找不到任何文件告诉我价值表达的确切含义(Apple的文档 - http://developer.apple.com/library /mac/#documentation/cocoa/conceptual/CoreDataVersioning/Articles/vmMappingOverview.html%23//apple_ref/doc/uid/TP40004735-SW3 - 只有一个非常简单的转换)。如果我必须做其他事情,那会是什么?这似乎很简单,表达式应该能够做到。

I can't find any document telling me exactly what is possible in the Value Expression (Apple's doc - http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreDataVersioning/Articles/vmMappingOverview.html%23//apple_ref/doc/uid/TP40004735-SW3 - only has a very simple transformation). If I have to do something else, what would that be? This seems simple enough that an expression should be able to do it.

推荐答案

您可以做的一件事是创建自定义迁移策略具有将属性从原始值映射到新值的函数的类。例如,我有一个案例,我需要映射一个名为MyItems的实体,该实体与一组名为Items的值实体有直接关系,而不是存储itemID,这样我就可以将模型拆分到多个商店。

One thing you can do is create a custom migration policy class that has a function mapping your attribute from the original value to a new value. For example I had a case where I needed to map an entity called MyItems that had a direct relationship to a set of values entities called "Items" to instead store an itemID so I could split the model across multiple stores.

旧模型看起来像这样:

The old model looked like this:

新模型如下所示:

The new model looks like this:

为此,我用一个名为itemIDForItemName的函数编写了一个映射类,它定义如下:

To do this, I wrote a mapping class with a function called itemIDForItemName and it was defined as such:

@interface Migration_Policy_v1tov2 : NSEntityMigrationPolicy {

  NSMutableDictionary *namesToIDs;
}

- (NSNumber *) itemIDForItemName:(NSString *)name;
@end






#importMigration_Policy_v1tov2 .h


#import "Migration_Policy_v1tov2.h"

@implementation Migration_Policy_v1tov2


    - (BOOL)beginEntityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error {

        namesToIDs = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1],@"Apples",
                      [NSNumber numberWithInt:2],@"Bananas",
                      [NSNumber numberWithInt:3],@"Peaches",
                      [NSNumber numberWithInt:4],@"Pears",
                      [NSNumber numberWithInt:5],@"Beef",
                      [NSNumber numberWithInt:6],@"Chicken",
                      [NSNumber numberWithInt:7],@"Fish",
                      [NSNumber numberWithInt:8],@"Asparagus",
                      [NSNumber numberWithInt:9],@"Potato",
                      [NSNumber numberWithInt:10],@"Carrot",nil];
        return YES;
    }
    - (NSNumber *) itemIDForItemName:(NSString *)name {

        NSNumber *iD = [namesToIDs objectForKey:name];

        NSAssert(iD != nil,@"Error finding ID for item name:%@",name);

        return iD;
    }
    @end

然后,对于属性的相关映射名称您的映射模型指定值表达式作为函数调用的结果:FUNCTION($ entityPolicy,itemIDForItemName,$ source.name)。您还必须将该属性的映射名称的自定义策略字段设置为映射类名称(在本例中为Migration_Policy_v1toV2)。

Then for the related Mapping Name for the attribute in your mapping model you specify the Value Expression as the result of your function call as such: FUNCTION($entityPolicy,"itemIDForItemName",$source.name) . You also have to set the Custom Policy Field of your Mapping Name for that attribute to your mapping class name (in this case Migration_Policy_v1toV2).

这篇关于核心数据迁移:属性映射值表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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