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?