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

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

问题描述

我的实体目前有一个 cardType 属性,在旧模型中可能是数学"、图像"或文本".在新模型中,我将只使用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").

最后,我有一组另一个实体,卡片",其中一组可以与卡片组相关联,并且在每个实体中,我还将拥有 hasImage,如果卡片组,我想将其设置为 true之前是图像"类型.

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.

旧模型如下所示:

新模型如下所示:

为此,我编写了一个映射类,其中包含一个名为 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


#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) **

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) **

您还必须将该属性的映射名称的自定义策略字段设置为您的映射类名称(在本例中为 Migration_Policy_v1tov2).

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

**注意这应该匹配方法的选择器签名

**note this should match the selector signature of the method

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

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