Master Data Transfer: Display Attribute Value

I currently have the cardType attribute, which in the old model could be "Math", "Image" or "Text". In the new model, I will use only "Math" and "Text", as well as the hasImage attribute, which I want to set to true if the old map type was Image (which I want to change to "Text").

Finally, I have a set of another entity, "card", from which the set can be associated with the deck, and in each of them I will also have hasImage, which I want to set to true if the deck was previously "Image".

Is this possible with the Value expression in the mapping model created between the two versions, or will I need to do something else?

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

+4
source share
1 answer

One thing you can do is create your own migration policy class, which has a function that maps your attribute from the original value to the new value. For example, I had a case where I needed to display an object called MyItems, which had a direct connection with a set of value objects called "Elements", instead, to store the itemID so that I could split the model into several stores.

The old model looked like this: old model

The new model looks like this: new model

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 

Then, for the associated mapping name for the attribute in your mapping model, the Value expression is called as the result of calling the function as such: FUNCTION ($ entityPolicy, "itemIDForItemName", $ source.name). You must also set the custom policy field of your mapping name for this attribute for your mapping class name (in this case, Migration_Policy_v1toV2).

Mapping model

+24
source

Source: https://habr.com/ru/post/1344418/


All Articles