Re: Core Data Versioning: Non-trivial Value Expressions?
Re: Core Data Versioning: Non-trivial Value Expressions?
- Subject: Re: Core Data Versioning: Non-trivial Value Expressions?
- From: Chaitanya Pandit <email@hidden>
- Date: Tue, 4 May 2010 16:20:28 +0530
Jerry,
Using just the mapping model seems okay if your Core Data) Model is simple, but most likely it would bloat over time and the best thing to do would be to customize the migration process. Actually it's not that hard and in fact makes sense.
To start with you create a subclass of NSEntityMigrationPolicy to say MyMigrationPolicy. In your mapping model you have to handle the mapping of say old entity "Foo" whose attribute "Bar" is of type Int16 in old model and Bool in new model. Now in you mapping model specify "MyMigrationPolicy" as the "Custom Policy" for attribute you wish to manage in a custom way.
Now implement the method:
- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)inSourceInstance
entityMapping:(NSEntityMapping *)inMapping
manager:(NSMigrationManager *)inManager
error:(NSError **)outError
In your MyMigrationPolicy class and deal with only the entities that you're interested in.
A sample implementation would look something like:
- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)inSourceInstance
entityMapping:(NSEntityMapping *)inMapping
manager:(NSMigrationManager *)inManager
error:(NSError **)outError
{
NSManagedObject *newObject;
NSEntityDescription *sourceInstanceEntity = [inSourceInstance entity];
if ( [[sourceInstanceEntity name] isEqualToString:@"Foo"] )
{
newObject = [NSEntityDescription insertNewObjectForEntityForName:@"Foo"
inManagedObjectContext:[inManager destinationContext]];
NSDictionary *keyValDict = [inSourceInstance committedValuesForKeys:nil];
NSArray *allKeys = [[[inSourceInstance entity] attributesByName] allKeys];
NSInteger i, max;
max = [allKeys count];
for (i=0 ; i< max ; i++)
{
// Get key and value
NSString *key = [allKeys objectAtIndex:i];
id value = [keyValDict objectForKey:key];
if ( [key isEqualToString:@"Bar"] )
{
[newObject setValue:[NSNumber numberWithBool:[value boolValue]] forKey:key];
}
else
[newObject setValue:value forKey:key];
}
}
[inManager associateSourceInstance:inSourceInstance
withDestinationInstance:newObject
forEntityMapping:inMapping];
return YES;
}
Thanks,
Chaitanya Pandit
Architect
Expersis Software Inc.
On May 4, 2010, at 7:23 AM, Jerry Krinock wrote:
> When doing Core Data versioning, when creating a Mapping Model, when writing Custom Value Expressions, I understand that the syntax to be used is that described in the Predicate Programming Guide > Predicate Format String Syntax.
>
> But I can't figure out how to do non-trivial mappings like these...
>
> Example 1: Old data model attribute type is an Integer16. I want to map this to a Boolean which is NO if the old value was 0 and YES if != 0.
>
> Example 2: Old and new data model attribute types are both String. If the string is "Joe", I want to map it to "Jose"; else, map the original value.
>
> It seems like the concept of "mapping" should support stuff like this, but I can't find any such examples. In MigratingDepartmentAndEmployees, all I see is mapping from one attribute name to another.
>
> Relations get a complicated expression like this generated automatically:
>
> FUNCTION($manager, "destinationInstancesForEntityMappingNamed:sourceInstances:" , "EmployeeToEmployee", $source.employees)
>
> but I have no idea where that FUNCTION keyword comes from (it's not listed as a reserved word in the Predicate Programming Guide). It seems like I should be able to make my own FUNCTION to implement the stuff I need in my examples. Is this possible?
>
> Thanks,
>
> Jerry Krinock
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden