5

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?

Tyler V
  • 9,694
  • 3
  • 26
  • 52
Naveed
  • 2,942
  • 2
  • 25
  • 58
  • 1
    No, I have already looked at the question, it handles the remove case which is working fine for me. I need to handle the `set` case and the `iterator` doesn't have a set method. – Naveed Dec 23 '21 at 03:24

1 Answers1

7

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()
}
Tyler V
  • 9,694
  • 3
  • 26
  • 52
  • Thanks Tyler for the answer. It works but any reason why `forEachIndexed` failed? – cgb_pandey Aug 16 '23 at 05:52
  • 1
    When you modify (mutate) an element in a MutableStateList it notifies observers to the list and presumably modifies/refreshes the list itself, which you can't do in a `forEach` loop (concurrent modification error). I haven't looked into the source code of MutableStateList, but is what is happening in general. – Tyler V Aug 16 '23 at 17:22