Search code examples
iosobjective-ccore-datacore-data-migration

How to Migrate an entity in Coredata where destination entity's model is just created


I have a model Version. The entity passPhrase of Version needs to be migrated to New entity passPhrase in Home model.

But the Home model is introduced in new version of database. It was not present in old xcdatamodel. How can I migrate data from

Version.passPhrase to Home.passPhrase

Solution

  • And the golden rule of CoreData migration is - avoid custom migration at any cost. :)

    So the way to do this to use lightweight migration to add your new entity, and then just use a one off migration script to move your data over.

    You can remove the passPhrase property from your Version class definition but you need to leave it in the model for now so you can still access the old data. This can be cleaned up at some time in the future when you are confident all of your users have upgraded to the newer version, or just leave it there forever.

    To access the existing value during your copy just use [version valueForKey:@"passPhrase"] and then once copied clear it out the same way [version setValue:nil forKey:@"passPhrase"]