Android的共享preferences限制? [英] Android SharedPreferences limitations?

查看:1277
本文介绍了Android的共享preferences限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我公司开发的游戏在Android上。我目前最节能的游戏状态的数据库。然而,应用程序不利用多于在DB的单个行。我现在在引进一些新的统计信息感兴趣,但是这将导致我的数据库重新安装,从而明确了每个人的进步。为了避免这种情况在未来我在考虑存储游戏统计与共享preferences代替。我的问题是很多不同的东西怎么可以存储这样,它成为一个问题之前。总共我将存储大约40值,所有的整数。

I developed a game on Android. I am currently saving most of the game stats in a Database. However the app does not utilize more than a single row in the DB. I am now interested in introducing some new stats but this will cause my DB to re-install and thus clear out everyone's progress. In order to avoid this in the future I am considering storing the game stats with SharedPreferences instead. My question is how many different things can be stored that way before it becomes a problem. In total I would be storing around 40 values, all integers.

推荐答案

共享preferences被写入到XML文件,所以在Android上的文件的最大尺寸为共享preferences XML文件有多大可能。我可以肯定地说,40的整数值将不会是一个问题。

SharedPreferences are written to xml files, so the maximum size of a file on Android is how large a SharedPreferences xml file can be. I can safely say that 40 integer values will not be a problem.

在共享preferences文件中的值的最大大小限制为您正试图存储值的最大尺寸。 (这意味着你不能把一个字符串值,该值大于字符串可以在Java中。)

The maximum size of a value in a SharedPreferences file is limited to the maximum size of the value you are attempting to store. (Meaning you can't put a String value that is longer than Strings can be in Java.)

我唯一建议是确保批量尽可能多的(意思是不要 .commit()每一个变化),也没有了修改为每个变化的新的编辑器。 (这只是良好做法。)

The only thing I would suggest is making sure to batch the edits as much as possible (meaning don't .commit() each change) and also don't create a new editor for each change. (These are just good practices.)

SharedPreferences settings = getSharedPreferences(PREFS_FILE_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("firstValue", mFirst);
editor.putInt("secondValue", mSecond);
editor.putInt("thirdValue", mThird);

// Commit the edits! (As infrequently as possible)
editor.commit();

这篇关于Android的共享preferences限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆