最好的方法保存数据 - preferences,sqlite的,序列化或其他?最好的、或其他、序列化、方法

2023-09-05 07:46:46 作者:仅限对你体贴

我一直在调查的匝间救了我的游戏数据的替代方法,并想知道如果任何人都可以在正确的方向指向我。

I've been investigating alternative methods for saving my game's data between turns, and wonder if anyone can point me in the right direction.

我有大约必须在onPause过程中保存的数据为32K。我排除了preferences由于数据的绝对数量。我花了几天的SQLite玩弄但无法得到的数据保存在不到两秒钟(虽然那时候当然没有白费)。

I have approximately 32k of data which must be saved during onPause. I ruled out preferences due to the sheer quantity of data. I spent a few days playing around with SQLite but couldn't get the data to save in less than two seconds (although the time certainly hasn't been wasted).

我已经决定,我将使用数据库在比赛一开始加载常量数据。这肯定会更容易地调整在游戏中的各种参数和默认值。但是,这仍然让我寻找理想的方法写入数据。

I've decided that I'll use the database for loading constant data at the beginning of the game. This will certainly make it easier to tweak various parameters and default values in the game. But this still leaves me looking for the ideal method for writing data.

这是需要保存的数据基本上是9出现A类和九个出现B类的我是一个密集的月份到了Android的学习曲线(和Java的细微差别,从C ++背景的)和一直在使用Google像疯了似的。这带来了两种可能性在脑海中 -

The data that needs to be saved is basically nine occurrences of class A and nine occurrences of class B. I'm an intensive month into the learning curve of Android (and the nuances of Java, coming from a C++ background) and have been googling like crazy. This brought two possibilities to mind -

1)序列化(ObjectOutputStream的)

1) Serialization (ObjectOutputStream)

我想这将是完美的解决方案,但是,看了就这一课题做一些其他职位,收集它没有强烈建议在Android平台上,由于速度和内存分配挑起垃圾收集到一个潜在的愤怒。

I thought this would be the perfect solution but, having read several other posts regarding the subject, gather that it isn't highly recommended on the Android platform due to speed and memory allocations provoking the garbage collector into a potential rage.

2)DataOutputStream类类

2) DataOutputStream class

我目前的想法是加载和保存功能添加到类和使用DataOutputStream类和的DataInputStream在其中呼吁分别写入和读取数据。

My current thought is to add Load and Save functions to both classes and to use DataOutputStream and DataInputStream calls in them to write and read the data respectively.

在类中的数据是原语(字符串和整数居多)和原语的数组,所以没有什么太复杂在那里打破。请问这第二个解决方案似乎不错,可行吗?还是有,我尚未对不知情?

The data in the classes are primitives (strings and ints mostly) and arrays of primitives, so there's nothing too complicated in there to break down. Would this second solution seem a good, viable one? Or are there other solutions that I am unaware of as yet?

推荐答案

您应该使用异步任务来保存数据,我用这个方法来获取高分在开始游戏:

You should use an Async task to save the data, I used this method to fetch highscores at the start a game:

new HighscoreTask().execute(this);

的异步任务看起来是这样的:

the Async task looks like this:

public class HighscoreTask extends AsyncTask<MainView, Void, Void> {

    protected void onPreExecute() {
    }

    protected void onPostExecute(final Void unused) {
    }
    @Override
    protected Void doInBackground(MainView... params) {
        HighScoreFactory.syncScores();
        return null;
    }
}

所有的数据库交互中的 HighScoreFactory.syncScores发生()的这可能需要只要需要,因为它发生在后台。在我的情况下,它发送一个HTTP请求到外部服务器,并加载到这些数据库。这是从来没有引起任何问题,并且可以无缝地。

All the database interaction happens in HighScoreFactory.syncScores() this can take as long as it needs because it happens in the background. In my case it sends an HTTP request to an external server and loads these into a database. It's never caused any problems and works seamlessly.