2

Ultimately I am trying to store an Int Array in Shared Preferences but I know Kotlin doesn't support that. So I am converting my Int Array to a String Array using the method here:

How can I store an integer array in SharedPreferences?

My issue is that I am struggling to put in a default value for the getStringSet method:

private fun loadIntScoreArray() {
        val prefs = getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE)
        //TODO: Load the String array
        var default = emptyList<String>()
        avgScoreArrayString = prefs.getStringSet(AVG_SCORE_ARRAY, default)
    }

However default is not an acceptable object in the prefs.getStringSet(AVG_SCORE_ARRAY, default) line. The error is confusing because it seems contradictory:

Required: MutableList

Found: (Mutable)Set!

Required: (Mutable)Set!

Found: MutableList

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Jared L.
  • 207
  • 2
  • 13

1 Answers1

1

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.

Sebastian Pakieła
  • 2,970
  • 1
  • 16
  • 24
  • Thank you. I am trying to store up to 5 previous scores and then find the average, so there is a good possibility there will be duplicate values. As you noted here I might run into challenges with that. Perhaps I can look at those libraries, but I was hoping for a simpler solution. It's too bad because in iOS Swift natively supports storing IntArrays to shared preferences. – Jared L. Oct 31 '19 at 11:32
  • If a case is to store 5 numbers, 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. – Sebastian Pakieła Oct 31 '19 at 12:40
  • Thank you, I ended up storing 5 independent Int values. It was a little tedious, but much easier than a database, lol. – Jared L. Nov 08 '19 at 01:59
  • Great to hear you have fixed your issue. It would be nice if you could mark the answer as accepted. – Sebastian Pakieła Nov 08 '19 at 09:59