Emre
08/04/2023, 9:40 PMmkrussel
08/04/2023, 9:49 PMFrancesc
08/04/2023, 9:51 PMI would avoid passing coroutine scopes to a function.while using
suspend
functions is preferable, there is a pattern where you either extend CoroutineScope
or pass the scope into the function, when the function launches a coroutine and returns immediately, without waiting for it to complete.
suspend
functions return when the work is complete, while functions extending CoroutineScope
or accepting a scope return immediatelyFrancesc
08/04/2023, 9:57 PMEmre
08/04/2023, 11:07 PMIf you need to launch a coroutine that keeps running after your function returns, then make your function an extension ofor passCoroutineScope
as parameter to make your intent clear in your function signature. Do not make these functions suspending.scope: CoroutineScope
Suspending functions, on the other hand, are designed to be non-blocking and should not have side-effects of launching any concurrent work. Suspending functions can and should wait for all their work to complete before returning to the caller³.