Obsolete
Status Update
Comments
dp...@gmail.com <dp...@gmail.com> #2
having this problem in api level 11 also
jo...@gmail.com <jo...@gmail.com> #3
This problem is still present on the 17 API level.
It is caused because the getStringSet() method of the SharedPreferences class doesn't return a copy of the Set object: it returns the entire object and, when you add new elements to it, the commitToMemory method of the SharedPrefencesImpl.EditorImpl class see that the existing value is equal to the previous one stored.
The ways to workaround this issue is to make a copy of the Set returned by SharedPreferences.getStringSet or force the write to memory using other preference that always change (for example, a property that stores the size of the set each time)
It is caused because the getStringSet() method of the SharedPreferences class doesn't return a copy of the Set object: it returns the entire object and, when you add new elements to it, the commitToMemory method of the SharedPrefencesImpl.EditorImpl class see that the existing value is equal to the previous one stored.
The ways to workaround this issue is to make a copy of the Set returned by SharedPreferences.getStringSet or force the write to memory using other preference that always change (for example, a property that stores the size of the set each time)
en...@google.com <en...@google.com>
mi...@gmail.com <mi...@gmail.com> #5
Why won't this bug be fixed? It's still present on API 33...
Description
SharedPreferences sp = this.getSharedPreferences("LocalCache", MODE_PRIVATE);
Set<String> items = sp.getStringSet("Items", new HashSet<String>());
items.add("#" + items.size() + ":This item can be saved to file?" );
SharedPreferences.Editor editor = sp.edit();
editor.putStringSet("Items", items);
editor.commit();
The expected result should be that another one item with different # was written to the file every time when the codes were executed.
The key cause should be I just modified the Set<String> object:items,which was got from SharedPreferences, and putted it back. And SharedPreferencesImpl only compares object reference to determine whether to save to file or not, the codes of line#413 in SharedPreferencesImpl.java is as following
if (existingValue != null && existingValue.equals(v)) { continue;}