androidkotlinconcurrentmodification

Modifying a SnapshotStateList throws ConcurrentModificationException


The documentation of SnapshotStateList states that it is similar to a regular mutable list. I have a use case where I need to modify all the elements in the list (set case). This does not change the size of the list, but I'm running into ConcurrentModificationException.

I have created a very simplified version of my usecase here. The following kotlin list works fine:

val myList2 = mutableListOf("a", "b", "c")
myList2.forEachIndexed { index, _ ->
    // Modify item at index
    myList2[index] = "x"
}

But I get a concurrent modification exception here:

val myList = mutableStateListOf("a", "b", "c")
myList.forEachIndexed { index, _ ->
    // Modify item at index but I get an exception
    myList[index] = "x"
}

How can I modify all elements of mutableStateList() in place without getting the concurrent modification exception?

Edit:

I can create a copy of the mutableStateList to iterate over which works fine but since I'm not changing the size of the list, is it possible to do it in place?


Solution

  • Some possible workarounds are to use replaceAll to transform the list in-place (as long as you don't need the index), or just use an old-fashioned loop over indices if you do

    val listA = mutableListOf("A","B","C")
    
    // this works
    listA.forEachIndexed { i, s ->
        listA[i] = s.lowercase()
    }
    
    val listB = mutableStateListOf("A","B","C")
    
    // this fails - as you noted
    listB.forEachIndexed { i, s ->
        listB[i] = s.lowercase()
    }
    
    // this works, as long as you don't need the index
    listB.replaceAll { s -> s.lowercase() }
    
    // this also works, and lets you have the index
    for(i in listB.indices) {
        listB[i] = listB[i].lowercase()
    }