I am searching for the maximum Android SharedPreferences key, value pairs but cannot find any good answer. Secondly, I want to as开发者_高级运维k that if I have a key what is its String value limit. How many character can put into it. If I need a choice for a value change frequently, should I use SQLite or SharedPreferences. Please refer me some good resources.
Yours,
All shared prefs are stored in /data/data/[package name]/shared_prefs/[app name].xml, so i think there's no limit based on aechitecture.
I don't know practical limit but i think it's enough for you..
Check Shared Preference for your reference.
should I use SQLite or SharedPreferences.
It is far better to use SQLite if you need to store values that will changes periodically. Also you can store more amount of data..
Following Android Developer reference it seems like key, value and node name have each a max value defined.
If you have to change many related key pairs, I'd recommend creating a simple SQLite database, but if you are using only a few pairs maybe SharedPreferences is not a bad and quick solution.
SharedPreferences are for quickly storing values like single strings, settings, flags, etc. The SQLite Database can do the same but is a little more heavy duty, think storing tables of information like customer data with multiple properties and being able to search (the Query in SQL).
So the answer is, "it depends" on what that "something" is you want to store? If it's a user setting then the SharedPreferences is quick and easy. If it's a set of records then the SQLite Database may make more sense.
As for the key size limit: I believe it is just the max size of a String. Also it is answered here: Shared Preferences - max length of a single value
The max key/value pairs limit: usingShared preferences these values are stored in .xml files as stated in the answer above, and you can have multiple .xml sharePreference files. The size limit I suppose is limited by the size of your app or the storage space available on the device using your app.
If your preference values change frequently, I would consider using local variables to keep track of current preference set, maybe a global singleton class. And save changes to disk before app is destroyed/closed. If you don't like this idea then try using SharedPreferences, but be sure to use the SharedPreferences.Editor.apply() instead of SharedPreferences.Editor.commit() to save preferences (apply saves to disk asynchronously, commit saves synchronously).
精彩评论