I found another post about this on here, but the solution did not work.
So I've got the following Kotlin code in a Singleton utility class
fun getAllFoodEvents(): MutableSet<String> {
return sharedPrefs.getStringSet(FOOD_EVENTS_KEY, HashSet<String>())
}
fun removeFoodEvent(foodEvent: FoodEvent) {
val events = getAllFoodEvents()
events.remove(foodEvent.toString())
saveAllFoodEvents(events)
}
And I've gone through all the following permutations of saveAllFoodEvents() and none of them have persisted changes across restarts. (I have tried switching out .apply() for .commit() at every stage as well)
fun saveAllFoodEvents(events: Set<String>) {
val editor = sharedPrefs.edit()
editor.remove(FOOD_EVENTS_KEY)
editor.putStringSet(FOOD_EVENTS_KEY, events)
editor.apply()
}
fun saveAllFoodEvents(events: Set<String>) {
val editor = sharedPrefs.edit()
editor.remove(FOOD_EVENTS_KEY)
editor.putStringSet(FOOD_EVENTS_KEY, HashSet<String>(events))
editor.apply()
}
fun saveAllFoodEvents(events: Set<String>) {
val editor = sharedPrefs.edit()
editor.remove(FOOD_EVENTS_KEY)
editor.putStringSet(FOOD_EVENTS_KEY, HashSet<String>())
editor.apply()
//I thought for sure this would work,
//considering it seems like it should be overkill
val editor1 = sharedPrefs.edit()
editor1.remove(FOOD_EVENTS_KEY)
editor1.putStringSet(FOOD_EVENTS_KEY, HashSet<String>(events))
editor1.apply()
}
However, I have been able to get it working with a very cumbersome workaround of saving all data in various shared preferences into local variables, calling editor.clear(), and then readding all the values.
Surely this is not the intended behavior of SharedPreferences?