Avoiding data loss due to interruption when saving files on android?

前端 未结 5 881
感动是毒
感动是毒 2021-02-07 11:24

I am wondering what strategies others use to avoid data loss when saving files on Android. I have a couple of game apps that and essentially, they potentially save the game stat

5条回答
  •  悲&欢浪女
    2021-02-07 12:05

    I think that saving little portions of data (not the whole game state) to the database every time user performs any action (not only when onPause() is called) would be the best approach in this case. However, this may require a lot of modifications to the code.

    The compromise would be to split the game state into smaller portions or sub-states (let's say, round1, round2, ..., players, etc) and again store data to appropriate files as user performs any action, not waiting for onPause() call. This would significantly reduce the probability of loss and at least would guarantee that the user doesn't loose the whole game progress. Moreover, to avoid inconsistent states of your app, which may appear when the save process is interrupted, you'd better save data to temporary file first and only in case of success simply rename the file, not copying its content (let's say, round1.xml.tmp rename to round1.xml).

提交回复
热议问题