Reputation: 354
Is it a good idea to store a list of String
with too many items in Sharedpreferences
? Does it hurt app performance?
I'm doing this to store:
public boolean saveArray(List<String> contacts) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(_context);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("Status_size", contacts.size());
for (int i = 0; i < contacts.size(); i++) {
editor.remove("Status_" + i);
editor.putString("Status_" + i, contacts.get(i));
}
return editor.commit();
}
And to read:
public void loadArray(Context context) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
_validContactsPhones.clear();
int size = sharedPreferences.getInt("Status_size", 0);
for (int i = 0; i < size; i++) {
_validContactsPhones.add(sharedPreferences.getString("Status_" + i, null));
}
}
Upvotes: 1
Views: 120
Reputation: 24241
No, its not a good idea to store a huge list of String
like contacts in SharedPreference
.
There's no specific limit of size for the stored SharedPreference
in your application as its stored in the /data/data/[package name]/shared_prefs/[app name].xml
. Since SharedPreferences are stored in an XML file it lacks the strong transaction support of SQLite. Hence I would recommend to store this kind of data in a SQLite database.
Another thing which needs to keep in mind that, the lowest size limit that I am aware of will be your amount of free heap space, as SharedPreferences reads that entire XML file's contents into memory.
The answer is mostly copied from here.
Upvotes: 1