Colton Idle
05/12/2023, 6:08 AMmyMap.value[MyType.A] = listOf(MyThing(1), MyThing(2))
myMap.value[MyType.B] = listOf(MyThing(3), MyThing(4))
myMap.value[MyType.C] = listOf(MyThing(5), MyThing(6))
with a map defined of
val myMap = _MutableStateFlow_<Map<MyType, List<MyThing>>>(_emptyMap_())
Chris Lee
05/12/2023, 6:37 AMVampire
05/12/2023, 6:38 AMMap
vs. MutableMap
Colton Idle
05/12/2023, 7:10 AMWout Werkman
05/12/2023, 9:58 AMMyType.A
entry into one map.
2. myMap.value
get's updated from another thread
3. You insert the MyType.B
and MyType.C
entries into the new map
4. The new map is now missing the MyType.A
entryval myMap = MutableStateFlow<Map<MyType, List<MyThing>>>(emptyMap())
myMap.update { oldValue ->
oldValue + mapOf(
MyType.A to listOf(MyThing(1), MyThing(2)),
MyType.B to listOf(MyThing(3), MyThing(4)),
MyType.C to listOf(MyThing(5), MyThing(6)),
)
}
Colton Idle
05/12/2023, 12:57 PMChris Lee
05/12/2023, 1:14 PMWout Werkman
05/12/2023, 1:46 PMupdate
myMap.update { it + (MyType.A to listOf(MyThing(1), MyThing(2)) }
You can wrap it in a utility function:
operator fun MutableStateFlow<Map<MyType, List<MyThing>>>.set(key: MyType, value: List<MyThing>): Unit = update { it + (key to value) }
myMap[MyType.A] = listOf(MyThing(1), MyThing(2))
Francesc
05/12/2023, 3:49 PM