Nikky
06/21/2024, 3:50 PMkevin.cianfarini
06/21/2024, 3:55 PMval flow: StateFlow<List<...>> = ...
flow.collectLatest { list ->
coroutineScope {
list.forEach { item ->
launch { item.doSomething() }
}
}
}
Nikky
06/21/2024, 3:56 PMross_a
06/21/2024, 3:56 PMstateFlow.collect { it.forEach { launch { task() } } }
Nikky
06/21/2024, 3:56 PMross_a
06/21/2024, 3:57 PMkevin.cianfarini
06/21/2024, 3:57 PMstateFlow.collect
will also conflate because state flow is always conflated for slow collectorsross_a
06/21/2024, 3:57 PMNikky
06/21/2024, 3:58 PMdecks.forEach {
it
.doThing()
.launchIn(flowScope)
}
ross_a
06/21/2024, 3:58 PMNikky
06/21/2024, 4:05 PMkevin.cianfarini
06/21/2024, 4:06 PMcollectLatest
will conflate less than collect
.kevin.cianfarini
06/21/2024, 4:06 PMkevin.cianfarini
06/21/2024, 4:07 PMcollectLatest
when you change the state of a state flow multiple times before it has a chance to emit those changes to flow collectorskevin.cianfarini
06/21/2024, 4:08 PMval sf = MutableStateFlow(0)
sf.value = sf.value + 1
sf.value = sf.value + 1
sf.value = sf.value + 1
In this scenario, you’re not guaranteed to get delivered 0, 1, 2, 3
because those updates might happen quicker than coroutine dispatch can occur. This happens regardless of the mechanism by which you collect this state flow.Nikky
06/21/2024, 4:09 PM