axl411
axl411

Reputation: 930

Consideration of multiple datamodel versions with Core Data light weight migration

I'm new to Core Data migrations. I read a tutorial about light weight migration. The scene is like this:

If my user updates quite often, it is alright. But what if my user has data model version-1, and updates the app directly to data model version-3? Do I need to write code to handle the migration from v1 to v3, or is it handled automatically for me since I used light weight migration?

Upvotes: 1

Views: 48

Answers (2)

Tom Harrington
Tom Harrington

Reputation: 70946

Your app needs to be able to handle all possible migrations that might happen. If the current version is the third, it must be possible to upgrade all previous versions to version 3.

This doesn't mean you actually have to write code for the migration. If automatic lightweight migration is possible, then it will work, without any custom migration code. Whether that works depends on how the model has changed. If a v1 --> v3 migration is possible with automatic lightweight migration, you can use that. If it's not possible with automatic lightweight migration, you need to handle it yourself. The answer depends only on how similar version 1 is to version 3, and has nothing to do with the fact that there was also a version 2.

Upvotes: 2

Mike Taverne
Mike Taverne

Reputation: 9352

In my experience this works automatically. It's fairly easy to test, also, so I would recommend that. Create v1, install app on device, add some data to the app. Create v2, v3, then run app on same device. Does it work?

Also, you need to be careful when writing new app code that you don't assume that the new fields have valid data in them for any existing records in the database at time of upgrade.

Upvotes: 1

Related Questions