I have a `mutableStateListOf<MyItem>` and I ...
# compose
c
I have a
mutableStateListOf<MyItem>
and I call
Copy code
state.myList.add(thing)
state.myList.sortBy { it.name }
but the item seems added and then sorted (not a huge list either. like 10 things) so the UI looks kinda jumpy. anything I can do for that?
e
You can try a binary search before adding each item and insert the item into the correct index
e
that's hard to do thread-safe if there's any other mutators
it's too bad there isn't a public atomic update method on SnapshotStateList or MutableState, it looks like they'd support it
I suppose
Copy code
val myList = MutableStateFlow(listOf<MyItem>())
myList.update { (it + thing).sortedBy { it.name } }
and observing
myList.collectAsState()
would be atomic
(maybe you don't need to worry about that)
e
I think you can make a mutable snapshot and do it in there. Or just synchronize because the critical section is really small. My initial preference would be to keep everything immutable, but not sure if that's a viable solution here.
☝️ 1
j
This is what Snapshot.withMutableSnapshot is for
today i learned 1
☝️ 1
e
oh that's a good point,
Snapshot.withMutableSnapshot { }
could help
c
That did the trick. thanks all
100 Views