Misbehavior When Trying to Store a String Set Using Sharedpreferences

Misbehavior when trying to store a string set using SharedPreferences

This "problem" is documented on SharedPreferences.getStringSet.

The SharedPreferences.getStringSet returns a reference of the stored HashSet object
inside the SharedPreferences. When you add elements to this object, they are added in fact inside the SharedPreferences.

That is ok, but the problem comes when you try to store it: Android compares the modified HashSet that you are trying to save using SharedPreferences.Editor.putStringSet with the current one stored on the SharedPreference, and both are the same object!!!

A possible solution is to make a copy of the Set<String> returned by the SharedPreferences object:

Set<String> s = new HashSet<String>(sharedPrefs.getStringSet("key", new HashSet<String>()));

That makes s a different object, and the strings added to s will not be added to the set stored inside the SharedPreferences.

Other workaround that will work is to use the same SharedPreferences.Editor transaction to store another simpler preference (like an integer or boolean), the only thing you need is to force that the stored value are different on each transaction (for example, you could store the string set size).

Shared Preferences not saving StringSet when application is killed (it's a feature)

After some unsuccessful searches I've found this - someone else had the same issue.

He solved this by removing the value and adding it again.

Further reading of the comments to his post revealed the cause, this is sharedPreferences.getStringSet documentation:

Note that you must not modify the set instance returned by this call. The consistency of the stored data is not guaranteed if you do, nor is your ability to modify the instance at all.

So I took a slightly different approach and created a new set as follows:

if (loadSubSet != null) {
loadSubSet = new HashSet<>(loadSubSet);
...

SharedPreferences putStringSet doesn't work

use edit.clear() before putStringSet

SharedPreferences ss = getSharedPreferences("db", 0);
Set<String> hs = ss.getStringSet("set", new HashSet<String>());
hs.add(String.valueOf(hs.size()+1));
Editor edit = ss.edit();
edit.clear();
edit.putStringSet("set", hs);
edit.commit();

Android: String set preference is not persistent

This has a ridiculous amount of duplicates - I bet that you do :

set = prefs.getStringSet("X", new HashSet<String>());
set.add("yada yada");
prefs.putStringSet("X", set);

In short android sees that set and the one inside refer to the same set and does nothing. Correct ?

See: Misbehavior when trying to store a string set using SharedPreferences

Trying to store mutableListOf String to SharedPreferences but cannot set default value

There is few things you need to know. Since API 11 you can only store plain objects or sets to shared preferences. You can convert your list to set, but it can be lossy conversion in your list contain duplicates.

If you want to use sets you should call it like this:

//to get
prefs.getStringSet(AVG_SCORE_ARRAY, emptySet<String>()))
//to set
prefs.edit().putStringSet(key, AVG_SCORE_ARRAY)

The other way is to join array to a single string using join operation. Here is a doc for you https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/join-to.html

To be honest both of these ways are not the perfect solutions. If it is production application and I recommend using persistance library like Room, Realm etc.
Hope it helps.

Edit.
If you are hundred percent sure you are going to have 5 ints stored (and it is not gonna change in a near or distant future), using database could be overkill. I recommend using joining to single string and storing it as single string or just store 5 independent int values. There is no point in complicating simple things.

SharedPreference Set String is resetting every time activity is recreated

Was searching for a solution for the same issue, a solution to this would be

// Get the current list.
SharedPreferences sp = getSharedPreferences("passes", 0);
SharedPreferences.Editor editor = getSharedPreferences("passes", 0).edit();
Set<String> passes = sp.getStringSet("myStrings", new HashSet<String>());

//Make a copy, update it and save it
Set<String> newPasses = new HashSet<String>();
newPasses.add(pass);
newPasses.addAll(passes);
editor.putStringSet("myStrings", newPasses); editor.commit();

android SharedPreferences putStringSet order/sort

This is not possible. Sets are unordered collections.



Related Topics



Leave a reply



Submit