Reputation: 649
I have an iPhone app that uses Core Data.
I did an update and used Lightweight Migration to go from V1 to V2 of my MOM (Managed Object Model). This worked perfectly.
What happens when I want to go to V3 (and beyond) of my MOM?
Another question is what is the best way to determine the version of the current MOM? Should I use isConfiguration:compatibleWithStoreMetadata:
Thanks for any assistance. I am loving Core Data. But it sometimes makes my head spin and I get confused, which is why I am seeking some sage wisdom.
Upvotes: 25
Views: 4282
Reputation: 4985
The initial posting was now many months ago, but I think that the best answer is found in Marcus Zarra's Core Data book (or online in the code examples). Google for "progressivelyMigrateURL" and one will find code for progressively iterating through models - which would allow one to create mappings from model n to model n+1, while not worrying about the combinatorial explosion for creating mappings between all pairings of models.
This may result in a slower migration at run time. I haven't investigated this.
Upvotes: 7
Reputation: 34253
I went with ordinary migration using createDestinationInstancesForSourceInstance.
The snippet shows how to override that method and how to get the sourceVersion of the model to migrate.
The actual migration is happening in the helper class TZMigrationHelper.
- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error
{
float sourceVersion = [[[mapping userInfo] valueForKey:@"sourceVersion"] floatValue];
if(sourceVersion <= 0.9)
{
mapping = [TZMigrationHelper addAttributeMappingForDerivedRTFProperties:sInstance mapping:mapping propertyName:@"someProperty"];
mapping = [TZMigrationHelper addAttributeMappingForDerivedRTFProperties:sInstance mapping:mapping propertyName:@"anotherProperty"];
mapping = [TZMigrationHelper addAttributeMappingForDerivedRTFProperties:sInstance mapping:mapping propertyName:@"oneMoreProperty"];
}
return [super createDestinationInstancesForSourceInstance:sInstance entityMapping:mapping manager:manager error:error];
}
Upvotes: 2