Iphone data migration on application update
I am working on an iphone application that uses sqlite to store application and user data.
This is the first version of the application and I wonder what I need to do to prepare for the future versions of the app.
Is this enough? 1. Make sure app version is correct in the XCode Project settings 2. Add a version number in sqlite file so that it can be read later
Lets simulate the next update! 1. New version of the app is downloaded to the device 2. The app reads the version number stored earlier in a sqlite table/field 3. App knows that application data has changed so it needs to update the sqlite and also it wants to retain the user data (in app favorite开发者_如何学编程s & Notes). 4. Schema has not changed so it starts to insert the new application data into some tables. 5. It does not touch the user data tables 6. It updates the version number of the sqlite db.
Have I missed something that will come back and bite me when it is time for update?
So, when you develop an application that uses Core Data you start defining a Core Data Model. Each model will have its own version. You start creating the first and unique version of your model. You can find all the required, detailed, info at the official Apple Core Data Model Versioning and Data Migration Programming Guide
You should not handle database versioning through custom fields set in the database itself, use model versions instead via your Xcode project.
When updating the application, if you do not change the model version you do not have to worry about anything, since the database hasn't been touched. Every new update that do not change the model will require no effort from you.
If you need to update your model for the next update you have to do this:
- Create a new model version
- Set the new model version as the active version
- Modify the model according to your needs
Here comes the interesting part. Your application will handle the migration between the models in two different ways: the simple way and the hard way.
Automatic lightweight migration
Following Apple's official documentation about Core Data Model Versioning and Data Migration, if your model has been slightly changed you can ask the application to perform an automatic lightweight migration. This step will update the existing database to the new version if these requirements are met:
- Simple addition of a new attribute
- A non-optional attribute becoming optional
- An optional attribute becoming non-optional, and defining a default value
The lightweight migration process will handle the appropriate required step to migrate from one model version to another, if possible
Mapping model migration
If you can't perform an automatic lightweight migration you will have to manually define the step to update the model versions to the new one, using Mapping Model Objects. These objects will define what the application has to do to migrate from model version x to model version y.
精彩评论