Consider a SQLite database for an iPhone app with some "documents" as templates. The user is able to add his own templates too.
I want to update the program and the database. Is there any chance to do so without overwriting the user's data?
I've considered two different databases, but since this is overhead, I want to avoid this option.
How can th开发者_C百科is be done without overwriting the user's data?
If you're using Core Data, then it should be relatively easy to add a new data model (schema) version and have 'migration' happen automatically. With some minor changes, you may need to creating a mapping model. And if your schema is changing significantly with entities being refactored, levels of abstraction being added, etc. then you may need to subclass NSEntityMigrationPolicy.
You might want to look at the Core Data Model Versioning and Data Migration Programming Guide
You might also look at Marcus Zarra's 'Core Data' book and sample code.
You could make your app check for old versions of the db when it runs and update it itself if necessary. So basically, update the app and it will handle updating the db. This would allow you to backup/restore data (if necessary).
When updating your application and its datastore, deliver ALTER
scripts in the upgrade package. When the app first runs, it should:
- check whether the database needs updating (version number, etc)
- run the
ALTER
andUPDATE
scripts to take schema and data from v1 to v2
精彩评论