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