简体   繁体   English

iOS推出应用更新。需要DB更新时保持用户数据完好无损

[英]iOS Rolling out app updates. Keeping user data intact when DB update required

I have just done a quick search and nothing too relevant came up so here goes. 我刚刚做了一个快速搜索,没有太多相关内容,所以这里就是这样。

I have released the first version of an app. 我已经发布了应用程序的第一个版本。 I have made a few changes to the SQLite db since then, in the next release I will need to update the DB structure but retain the user's data. 从那时起,我对SQLite数据库进行了一些更改,在下一个版本中,我将需要更新数据库结构,但保留用户的数据。

What's the best approach for this? 对此最好的方法是什么? I'm currently thinking that on app update I will never replace the user's (documents folder, not in bundle) database file but rather alter its structure using SQL queries. 我目前正在考虑在应用程序更新时,我永远不会替换用户的(文档文件夹,而不是捆绑)数据库文件,而是使用SQL查询更改其结构。

This would involve tracking changes made to the database since the previous release. 这将涉及跟踪自上一版本以来对数据库所做的更改。 Script all these changes into SQL queries and run these to bring the DB to the latest revision. 将所有这些更改编写到SQL查询中并运行这些更改以使DB进入最新版本。 I will also need to keep a field in the database to track the version number (keep in line with app version for simplicity). 我还需要在数据库中保留一个字段来跟踪版本号(为简单起见,与app版本保持一致)。

Unless there are specific hooks, delegate methods that are fired at first run after an update I will put calls for this logic into the very beginning of the appDelegate, before anything else is run. 除非有特定的钩子,委托在更新后首次运行时触发的方法,我将把这个逻辑的调用放到appDelegate的最开头,然后运行其他任何东西。

While doing this I will display "Updating app" or something to the user. 在执行此操作时,我将向用户显示“正在更新应用”或其他内容。

Next thing, what happens if there is an error somewhere along the line and the update fails. 接下来,如果沿线某处出现错误并且更新失败,会发生什么。 The DB will be out of date and the app won't function properly as it expects a newer version? 数据库将过时,应用程序将无法正常运行,因为它需要更新的版本?

Should I take it upon myself to just delete the user's DB file and replace it with the new version from the app bundle. 我应该自己删除用户的数据库文件并将其替换为应用程序包中的新版本。 OR, should I just test, test, test until everything is solid on my side and if an error occurs on the user's side it's something else, in which case I can't do anything about it only discard the data. 或者,我应该只测试,测试,测试,直到一切都在我身边,如果在用户身上发生错误,那就是其他东西,在这种情况下,我无法做任何事情只会丢弃数据。

Any ideas on this would be greatly appreciated. 任何有关这方面的想法将不胜感激。 :) :)

Thanks! 谢谢!

First of all, the approach you are considering is the correct one. 首先,您正在考虑的方法是正确的。 This is known as database migration. 这称为数据库迁移。 Whenever you modify the database on your end, you should collect the appropriate ALTER TABLE... etc. methods into a migration script. 无论何时修改数据库,都应该将相应的ALTER TABLE...等方法收集到迁移脚本中。

Then the next release of your app should run this code once (as you described) to migrate all the user's data. 然后,应用程序的下一个版本应运行此代码一次(如您所述)以迁移所有用户的数据。

As for handling errors, that's a tough one. 至于处理错误,这是一个艰难的。 I would be very weary of discarding the user's data. 我会非常厌倦丢弃用户的数据。 Better would be to display an error message and perhaps let the user contact you with a bug report. 更好的方法是显示错误消息,并让用户通过错误报告与您联系。 Then you can release an update to your app which hopefully can do the migration with no problems. 然后,您可以发布应用程序的更新,希望可以毫无问题地进行迁移。 But ideally you test the process well enough that there shouldn't be any problems like this. 但理想情况下,您应该对此过程进行充分测试,以免出现此类问题。 Of course it all depends on the complexity of the migration process. 当然,这一切都取决于迁移过程的复杂性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM