17

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 state/savegame whenever the user pauses (onPause).

This works in 99.99% of the cases, but every once in a while I receive an example of a savegame where the saving process has gone wrong. Usually, this is an ill-formed XML file, typically truncated at some arbitrary point. Based on the bug reports I receive, I believe that the problem mostly occurs when the user is interrupted during gameplay by a phone call or something like that and the Android OS then kills the app before it can finish saving. The code for saving the files is pretty simple, so I have difficulty seeing what else might cause this issue.

This is a serious problem, because it will usually result in the player's save progress being ruined.

I was thinking of writing to an empty file first, and then copying to the "real" file only after, but I suspect that would simply increase the problem as it will overall take more time and still risks getting interrupted.

Anyone who has a secure way of doing this that Android is relatively guaranteed not to mess up?


So summarized, the options suggested so far (as I understand them):

  1. Use a service for the saving process, on the assumption that this will be less likely to be killed by the OS.
  2. Save in temp file; copy over when save is verified.
  3. Incremental saving of the game state (I actually already use this for player log information).
  4. Combination of 2 & 3.
  5. Move the saving to another thread, as the problem may be with ANR kills [DC's comments below].

I do not think SharedPreferences will work for this kind of structured data, At the moment, neither of these methds seem like an ideal solution, so I am still open to suggestions.


I've not yet managed to test all of these approaches, so rather than waste the bounty, I have assigned it to the answer that I feel would be most likely to solve the issue. I plan to check through the various options still, though, prior to accepting an answer. Thanks for all of the good suggestions.

Michael A.
  • 4,163
  • 3
  • 28
  • 47
  • Is it too much data to be saved to `SharedPreference`? – Ron May 06 '12 at 13:54
  • Yes - about 20K of data in XML. This may not be a major issue (haven't tried), but the rather complex structure of the data would be, I think. – Michael A. May 07 '12 at 23:11
  • How are you saving the game state in `onPause()`? Are you doing I/O on the main (UI) thread? Where are you saving the game state (on SD-card or other location)? – David Wasser May 09 '12 at 08:42
  • Yes, it's usually done onPause and directly from the function (main thread). Have never bothered to move it into a thread since save time has never been an issue (that I know of), except for (presumably) this issue. – Michael A. May 09 '12 at 08:52
  • 2
    I would add: *5. Move the saving to another thread.* to your list. You say it occurs 0.01% of the time, which would lead me to believe it is the issue where some Android devices don't support concurrent files system access. If you block the UI thread on any application, Android will kill it after a few seconds. If you thread it, Android will have no reason to kill it because the UI thread is not blocked. Now, you should/might wait for the thread in `onFinish()` if possible. But in the case of killing an application, Android doesn't even guarantee that `onFinish()` will be called. – dcow May 09 '12 at 11:22
  • I do actually know precisely how often this occurs; I'm just guessing based on the very rare reports I receive. It is probably much less than 0,01% (we're talking 10K or more sessions a day), but this is certainly an interesting idea. Definitely worth looking into. – Michael A. May 10 '12 at 09:47

5 Answers5

4

This sounds like a good job for a service.

http://developer.android.com/reference/android/app/Service.html

ian.shaun.thomas
  • 3,468
  • 25
  • 40
  • As I understand it, a Service can also be force-killed though, and is connected to the process which started it. Not sure this will help much, in that case, since the problem appears to be caused by Android OS force-killing the app. – Michael A. Apr 23 '12 at 21:00
  • A service runs in the background, the OS will be defiantly be killing your app. Maybe you should consider this suggestion ... – Kickaha May 04 '12 at 23:33
  • Anything can be killed by the OS, however, I still believe a service is what you want to use. – ian.shaun.thomas May 07 '12 at 20:46
  • To the service is the same if your app is still running or not, it will save your data in the background. From android doc: `Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.` – Axxiss May 10 '12 at 06:27
  • Yes, but this does not help a lot. For one, it will still be killed if memory is low. It being able to restart and recover again does not really help, because the entire purpose of doing it is to save the state of the app (which would now be lost). The only obvious advantage I can really see to this is that it bumps the priority of the hosting process, which may conceivably keep the whole thing running just long enough to finish the job. But if that is all that is needed, surely there must be better ways to do it. – Michael A. May 10 '12 at 09:39
4

We've had occasional issues when we're doing I/O (usually writing) to persistent storage (private filesystem on internal memory) from the main thread. Usually this doesn't take much time at all, but occasionally it inexplicably takes ages (20 or 30 seconds or more). It seems that the Android filesystem implementations on some devices don't support concurrent access (see this, so your I/O can block if another process is using the filesystem. If you are doing your I/O on the main thread, the OS can/will kill your Activity if it blocks for too long. This may be what is happening to you.

Due to this problem, I would suggest that you move all your I/O to a separate thread (not the main thread). So, for example, to save the game state, in onPause() call a method that serializes the game state to a ByteArrayOutputStream which you then hand off to a separate thread to be written eventually to the filesystem.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
3

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).

a.ch.
  • 8,285
  • 5
  • 40
  • 53
1

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.

HardCoder
  • 3,026
  • 6
  • 32
  • 52
  • It's hard to be sure, since I am unable to recreate the situation myself. But all the confirmed reports I've received with this kind of problem has typically been cases where the user has been interrupted by a phone call or something similar. – Michael A. May 07 '12 at 23:14
  • I am fairly certain that there is no error with the saving itself; the XML API should not be able to write truncated files of this type under normal circumstances. – Michael A. May 07 '12 at 23:17
0

If possible use SharedPreferences to store your game state. Your changes will be committed only after you call commit(). You can do the saving in onSaveInstanceState callback. Read the docs here.

Ron
  • 24,175
  • 8
  • 56
  • 97