避免数据丢失,由于中断在Android上保存文件时?数据丢失、保存文件、Android

2023-09-05 02:09:58 作者:挽梦亦清歌

我想知道什么样的战略他人使用在Android上保存文件时,以避免数据丢失。我有一对夫妇的游戏应用程序这一点,从本质上讲,他们可能挽救了比赛状态/秘技每当用户暂停(的onPause)。

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

本工程的情况下,99.99%,但每过一段时间,我收到一个秘技,其中保存过程出了问题的一个例子。通常情况下,这是一种不良的XML文件,通常被截断某任意点。根据我收到的bug报告,我相信,当用户的游戏通过电话或类似的东西和Android操作系统,然后杀死应用程序,才能完成减排过程中被中断多发生的问题。在code保存文件为pretty的简单,所以我很难看到还有什么可能会导致此问题。

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.

任何人谁都有这样的安全方式,Android是相对保证不会弄乱?

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

使用服务的保存过程,前提是这将是不太可能由OS杀死。 保存在临时文件;副本保存时验证过。 在增量减排的游戏状态(其实我已经使用这个供玩家日志信息)。 的2和组合; 3。 将在保存到另一个线程,因为该问题可能与ANR杀死[以下DC的意见。

我不认为共享preferences将努力为这种结构化的数据,目前,无论这些methds的似乎是一个理想的解决方案,所以我还是愿意接受建议。

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.

推荐答案

我觉得保存数据的小部分(不整场比赛状态)到数据库中,每次用户进行任何操作(不仅在的onPause()称)将在这种情况下,最好的办法。然而,这可能需要大量的修改到code

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.

妥协将是本场比赛的状态分裂成更小的部分或子状态(比方说, ROUND1 round2 的,......,的 播放器 的,等等),并再次存储数据作为用户进行任何操作,而不是等待的onPause()调用相应的文件。这将显著减少丢失的概率和至少将保证用户不失去整个游戏进行。另外,为避免您的应用程序不一致的状态,这可能会出现在保存过程被中断,你最好首先将数据保存到临时文件,并只有在成功的情况下简单地重命名的文件,而不是复制其内容(比方说, round1.xml.tmp 的重命名为 round1.xml 的)

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

 
精彩推荐
图片推荐