iPhone and Core Data: how to retain user-entered data between updates?

前端 未结 3 1466
滥情空心
滥情空心 2021-02-10 11:43

Consider an iPhone application that is a catalogue of animals. The application should allow the user to add custom information for each animal -- let\'s say a rating (on a scale

3条回答
  •  既然无缘
    2021-02-10 12:27

    Storing your SQLite database in the Documents directory (NSDocumentDirectory) is certainly the way to go. In general, you should avoid application changes that modify or delete SQL tables as much as possible (adding is ok). However, when you absolutely have to make a change in an update, something like what Amorya said would work - open up the old DB, import whatever you need into the new DB, and delete the old one.

    Since it sounds like you want a static database with an "Animal" table that can't be modified, then simply replacing this table with upgrades shouldn't be an issue - as long as the ID of the entries doesn't change. The way you should store user data about animals is to create a relation with a foreign key to an animal ID for each entry the user creates. This is what you would need to migrate when an upgrade changes it.

提交回复
热议问题