Nthily
07/02/2024, 2:22 PMfun main() = runBlocking {
val coroutineScope = this
val mainFlow = flow {
kotlinx.coroutines.delay(1000)
emit("String")
}
fun createDependentFlows(value: String): Pair<Flow<Int>, Flow<Boolean>> {
val flow1 = flow {
emit(value.length)
}
val flow2 = flow {
emit(value.isNotEmpty())
}
return Pair(flow1, flow2)
}
val combinedFlow = mainFlow.flatMapLatest { mainValue ->
val (flow1, flow2) = createDependentFlows(mainValue)
combine(
flow1,
flow2
) { value1, value2 ->
Pair(value1, value2)
}
}.stateIn(coroutineScope, SharingStarted.Eagerly, null)
combinedFlow.collect {
println(it)
}
}
Why is it that if i add .stateIn(coroutineScope, SharingStarted.Eagerly, null)
, the combinedFlow will not collect? 🤔Sam
07/02/2024, 2:28 PMstateIn
is that the flow won't terminate. It still emits the values, but after that, the program will run forever. In the Kotlin Playground, the buffering of output could mean that it times out before you see any console output.Nthily
07/02/2024, 2:38 PMSam
07/02/2024, 2:39 PMstateIn(…)
line is present or not.Nthily
07/02/2024, 3:12 PM