Hi all, Did anyone try using SnapShotStateMap to r...
# compose-android
a
Hi all, Did anyone try using SnapShotStateMap to render items in LazyColumn? SnapShotStateMap implements MutableMap and hence it doesn't follow order. Does anyone know any workaround to make the SnapShotStateMap into an ordered map?
s
What does it being a MutableMap have to do with how it's ordered?
z
Guaranteed insertion order is a property of LinkedHashMap which is what I believe MutableMap uses on jvm. SnapshotStateMap uses kotlinx.immutable persistent map, which I guess doesn’t have that property?
a
Yes, SnapshotStateMap uses MutableMap implementation and doesn't follow any order and hence I couldn't able to use the map inside the lazycolumn items.
z
You could store IDs in a list for ordering. it’s 2 data structures instead of one, and twice the state reads, but pretty simple to implement
a
BTW you can also use
SnapshotStateList
if using
SnapShotStateMap
is not required
z
Yea, store ids in a SnapshotStateList
a
Well if i store Ids in a snapshotstatelist, it can't be used for smart recomposition since the Ids won't be changed and hence there isn't any need to have a SnapShotStateList. If i pass the Ids to the LazyColumn, how will it possibly know that a particular item is changed and has to be recomposed?
a
Store Pair(id, item) in the list.. here issue is that not all types of collection is provided by the
Snapshot
Alternatively you can use two different things.. one for ordering(
SnapShotStateList
) and one for values
SanpShotStateMap
and then you can observer both
👍🏻 1
z
Store the ids in a list, then each item in your lazy column looks up the rest of the data for that item (eg from a SnapshotStateMap)
129 Views