Felix
10/31/2018, 9:09 PMcoroutineScope{ launch {...} launch {...}}
. However, coroutineScope
suspends until all the children coroutines are completed. Probably most of the times this is a good thing, however there may be situations where I want to continue without waiting for the child coroutines to end.marstran
10/31/2018, 10:17 PMlaunch {
val scope = this
launch {
val job = scope.launch {
// Do something
}
} // Will not wait for "job" here.
} // It will wait for "job" here.
Felix
10/31/2018, 10:41 PMsuspend
function, without creating a new scope. A way would be to explicitly pass the CoroutineScope
explicitly into the suspend
function, however that seems odd.suspend
(via the coroutineContext
intrinsic), however the coroutine scope doesn't seem to be available that way.gildor
10/31/2018, 11:19 PMA way would be to explicitly pass theIt's not odd and recommended way to write such functions as extensions of CoroutineScopeexplicitly into theCoroutineScope
function, however that seems odd.suspend
Felix
11/01/2018, 12:04 AMsuspend
functions in the path until the inner launch now need to be CoroutineScope extensions, so that this scope is explicitly passed?gildor
11/01/2018, 1:12 AMcoroutineScope
builder, but still completely safe
however there may be situations where I want to continue without waiting for the child coroutines to endIf you just want to run a new global coroutine just use
GlobalScope
, but this is explicit now because you say “yes, this coroutine doesn’t have automatic lifecycle management and bindings to parent lifecycle”, but this should be pretty rare case (in most cases you should and can avoid it), but it still possible and will work the same way as coroutines worked before structured concurrency
If you have some real life example, please do not hesitate to share ituhe
11/01/2018, 9:57 AMGlobalScope
is by definition bad. I don't see it that way.
For example in our app there are many instances where you start a coroutine as a reaction to a user's command (e.g. "play this music") which you really don't want to automatically cancel when the current scope (e.g. a presenter) is left, so you start them using GlobalScope.launch()
.
Indeed, I don't have a single invocation of coroutineScope
in our whole project, because it's somehow never really what I need.gildor
11/01/2018, 10:00 AMcoroutineScope
is very helpfuluhe
11/01/2018, 10:05 AMgildor
11/01/2018, 10:11 AMfun playMusic(): Unit
?uhe
11/01/2018, 10:15 AMgildor
11/01/2018, 10:42 AM