Christopher Porto
09/19/2022, 5:29 PMsuspend fun func1() = CoroutineScope(Dispatchers.Default).launch {
func2()
println("Not called")
}
suspend fun func2() = coroutineScope {
launch {
someFlow.collect { logic.invoke(it) }
}
}
Eric Chee
09/19/2022, 5:41 PMShared flow never completes. A call to Flow.collect on a shared flow never completes normally, and neither does a coroutine started by the Flow.launchIn function. An active collector of a shared flow is called a subscriber.
Christopher Porto
09/19/2022, 5:49 PMCasey Brooks
09/19/2022, 5:49 PMcoroutineScope { }
will suspend as long as there are any other coroutines still running it in. Since the MutableSharedFlow never completes as mentioned above, the launch { }
will continue to run forever, and thus the coroutineScope { }
will also suspend indefinitelyChristopher Porto
09/19/2022, 5:51 PMCasey Brooks
09/19/2022, 5:52 PMfun CoroutineScope.func2() = launch { }
. a function marked suspend
implies that it will run until whatever inside it is finished. In contrast, by removing suspend
and requiring a CoroutineScope
receiver, you’re implying that the method is a “fire-and-forget” that will not suspend execution of the other stuff in the spot where it is calledChristopher Porto
09/19/2022, 5:54 PM