I've used shared preferences many times, but for some reason, changes aren't being saved in a new app that I'm testing. Here's a snippet of the important code:
SharedPreferences sp = getSharedPreferences(getString(R.string.key_preferences), MODE_PRIVATE);
Set<String> widgets = sp.getStringSet(getString(R.string.key_widgets), (new HashSet<String>()));
widgets.add(name + " " + Integer.toString(appWidgetId) + " " + address);
sp.edit().putStringSet(getString(R.string.key_widgets), widgets).commit();
I've used logging to check that the widget is added to the set, but the updated set is never saved. If I change the last line to...
sp.edit().putStringSet(getString(R.string.key_widgets), widgets).putString("testkey", "testvalue").commit();
...then everything saves just fine. What am I missing?
*UPDATE:
I found out that this also works:
SharedPreferences sp = getSharedPreferences(getString(R.string.key_preferences), MODE_PRIVATE);
Set<String> widgets = sp.getStringSet(getString(R.string.key_widgets), (new HashSet<String>()));
Set<String> newWidgets = new HashSet<String>();
for (String widget : widgets) newWidgets.add(widget);
newWidgets.add(name + " " + Integer.toString(appWidgetId) + " " + address);
sp.edit().putStringSet(getString(R.string.key_widgets), newWidgets).commit();
Perhaps I missed something in the documentation about needing to create a new object for the editor to save the prefs.
*UPDATE 2:
It makes no difference if I create an editor object:
SharePreferences.Editor spe = sp.edit();
spe.putStringSet(getString(R.string.key_widgets), widgets)
spe.commit();
We just need to read the documentation more carefully
According to getStringSet
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.
In fact it should have been noted in SharedPreferences.Editor not to send putStringSet a set that may modified afterwards. Make a copy of the set returned from getStringSet before modifying, and make a copy of your set before sending it to putStringSet.
SharedPreferences myPrefs = getSharedPreferences(myPrefName, MODE_PRIVATE);
HashSet<String> mySet = new HashSet<string>(myPrefs.getStringSet(mySetKey, new HashSet<string()));
....
SharedPreferences.Editor myEditor = myPrefs.edit();
Then one of
myEditor.putStringSet(mySetKey, new HashSet<String>(mySet));
or
myEditor.putStringSet(mySetKey, (Set<String>) mySet.clone());
I would like to improve Chike's answer a bit.
The reason why commit() did not save to disk is obvious if we take a look at the implementation(unchanged since v4.0.1), which commit to memory first and then waits for disk writes to complete:
public boolean commit() {
MemoryCommitResult mcr = commitToMemory();
SharedPreferencesImpl.this.enqueueDiskWrite(
mcr, null /* sync write on this thread okay */);
try {
mcr.writtenToDiskLatch.await();
} catch (InterruptedException e) {
return false;
}
notifyListeners(mcr);
return mcr.writeToDiskResult;
}
The point is, no changes will be detected if we call putStringSet() with the same instance from getStringSet():
// Returns true if any changes were made
private MemoryCommitResult commitToMemory() {
MemoryCommitResult mcr = new MemoryCommitResult();
...
for (Map.Entry<String, Object> e : mModified.entrySet()) {
String k = e.getKey();
Object v = e.getValue();
...
if (mMap.containsKey(k)) {
Object existingValue = mMap.get(k);
if (existingValue != null && existingValue.equals(v)) {
continue;
}
}
mMap.put(k, v);
}
mcr.changesMade = true;
...
}
...
}
}
return mcr;
}
Since there is no change made, writeToFile() gladly skips disk writing and set a successful flag:
private void writeToFile(MemoryCommitResult mcr) {
// Rename the current file so it may be used as a backup during the next read
if (mFile.exists()) {
if (!mcr.changesMade) {
// If the file already exists, but no changes were
// made to the underlying map, it's wasteful to
// re-write the file. Return as if we wrote it
// out.
mcr.setDiskWriteResult(true);
return;
}
...
}
...
}
Please refer to Chike's answer, make a copy of the String set before saving it to the same SharedPreference key.
You need to save the Editor object and then call commit() (before Android 2.3) or apply() (for Android 2.3 and above).
SharedPreferences.Editor editor = sp.edit();
editor.put...
editor.commit();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With