George
11/24/2022, 5:19 PMopen class WrapperFlow {
private val hotFlow = MutableSharedFlow<Int>()
fun getFlow(): Flow<Int> = hotFlow
@Transactional
open suspend fun emit() {
hotFlow.emit(10)
}
}
suspend fun main() = coroutineScope {
println("hi")
val wrapperFlow = WrapperFlow()
val job1 = launch {
wrapperFlow.getFlow().collect {
println("job1: $it")
}
}
wrapperFlow.emit() // does not print job1: $it
job1.cancelAndJoin()
}
Thanks in advance for any answers !George
11/24/2022, 5:36 PMfranztesca
11/24/2022, 6:32 PMGeorge
11/25/2022, 6:50 AMjoin
instead of cancelAndJoin()
George
11/25/2022, 7:00 AMsuspend fun main(): Unit = coroutineScope {
println("hi")
val wrapperFlow = WrapperFlow() // init flow
val job1 = launch {
wrapperFlow.getFlow().collect {
println("job1: $it")
}
}
println("subs: ${wrapperFlow.getFlow().subscriptionCount.value}") // 0
launch {
wrapperFlow.emit() // does print job1: $it
}
wrapperFlow.emit() // does print job1: $it
}
But like this it does not prints anything
suspend fun main(): Unit = coroutineScope {
println("hi")
val wrapperFlow = WrapperFlow() // init flow
val job1 = launch {
wrapperFlow.getFlow().collect {
println("job1: $it")
}
}
println("subs: ${wrapperFlow.getFlow().subscriptionCount.value}") // 0
// launch {
// wrapperFlow.emit() // does print job1: $it
// }
// wrapperFlow.emit() // does print job1: $it
wrapperFlow.emit() // does not print job1: $it if the above is commented out
}