0

I am writing an Android app and have to save a StringSet as a SharedPreference. The relevant code looks like this:

dialog.setButton(DialogInterface.BUTTON_POSITIVE, if (remove) "Entfernen" else "Hinzufügen") { _, _ ->
        val courses = prefs.getStringSet("filters", mutableSetOf())!!
        if (remove) {
            if (!courses.remove(course)) {
                Toast.makeText(activity, "Konnte \"$course\" nicht entfernen", Toast.LENGTH_LONG).show()
                return@setButton
            }
        } else if (!courses.add(course)) {
            Toast.makeText(activity, "Konnte \"$course\" nicht hinzufügen", Toast.LENGTH_LONG).show()
            return@setButton
        }
        dialog.dismiss()
        val editor = prefs.edit()
        editor.putStringSet("filters", courses)
        Log.d("RecyclerViewAdapter", "Committed changed prefs: ${editor.commit()}") //TODO("doesn't apply permanently")
        Toast.makeText(activity, "\"$course\" ${if (remove) "aus Filterliste entfernt" else "zu Filterliste hinzugefügt"}", Toast.LENGTH_SHORT).show()
    }

The Logcat output looks like this:

RecyclerViewAdapter: Committed changed prefs: true

In the documentation the following is written:

Returns true if the new values were successfully written to persistent storage.

Thus, I would assume that the changes are written to the persistent storage and when requesting the SharedPreference in the same app process, the changes are there.
However, if I restart the app, the SharedPreference has its original value again and by checking the file where the SharedPreferences are stored (/data/data/[AppID]/shared_prefs/[AppID]_preferences.xml) I was able to confirm that the changes were never written to persistent storage which contradicts what's written in the documentation.

Has anyone encountered this issue? What is causing this? How can I fix it?

GreenSmurf
  • 210
  • 1
  • 9
  • try to perform the editor.commit() before the log.d – Firas Shrourou Jan 31 '19 at 17:54
  • How are you creating the `courses` `Set`? – Mike M. Jan 31 '19 at 17:54
  • @MikeM. I've added a bit more of the code – GreenSmurf Jan 31 '19 at 17:59
  • Is it possible that when you restart the app you overwitter whatever is set in sharedpreferences? – Juan Jan 31 '19 at 18:00
  • 1
    OK, that's your problem. You can't modify the `Set` returned by `getStringSet()` and then write that same object back. You need to create a new a `Set` object first, modify that, and then save that new one back. It's kinda tricky, and certainly non-obvious, unless you happened to have read the docs for that particular method. – Mike M. Jan 31 '19 at 18:00
  • 1
    @MikeM. Thanks! I've changed ``val courses = prefs.getStringSet("filters", mutableSetOf())!!`` to ``val courses = prefs.getStringSet("filters", mutableSetOf())!!.toMutableSet()`` and it works now. You might consider posting this as an official answer so I can mark the question as solved. – GreenSmurf Jan 31 '19 at 18:08
  • 1
    Actually, I've already closed this as a duplicate. It's a common issue; just had one yesterday, too, as a matter of fact. Thank you, though. I appreciate the offer. Glad you got it fixed. Cheers! – Mike M. Jan 31 '19 at 18:10

0 Answers0