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

前端 未结 5 882
感动是毒
感动是毒 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 11:58

    First off I would try to see if the app is really killed without notification because I don't think that should be the case. Apps might be stopped for something like a phone call but I don't really think android is just killing it without any notification at all. It could decide to end it, but not without saving the state, etc.

    Maybe there is just an error that occurs with the saving itself.

    A way that does not require much modification to check that would be to use a checksum or use some other way to verify the integrity of the saved data (size, ending marker, etc.). All you have to do is save it, read it and check it (log any mistakes) and repeat that until its correct. It might not be that good if you save large amounts of data, but for mere gamedata it should be all right.

    You could also set a state when the app starts and ends, and then you would know on the next start if it ended correctly the last time or not and if you have to take extra measures.

提交回复
热议问题