Chills
02/16/2020, 1:58 PMco-routine scopes
for coroutines
can someone explain me in layman
terms.
builders are used to create coroutines
dispatchers to alter context.Adam Powell
02/16/2020, 3:34 PMCoroutineContext
is more or less an immutable Map
and CoroutineScope
is a holder for such a map. The intent comes from what standard elements commonly get held in that map: Job
and ContinuationInterceptor
.Adam Powell
02/16/2020, 3:35 PMAdam Powell
02/16/2020, 3:37 PMCoroutineScope
. The async with nursery
pattern shown there is structurally the same as kotlin's coroutineScope {}
blocks.streetsofboston
02/16/2020, 4:21 PMCoroutineScope.launch
or CoroutineScope.async
, which builds a Coroutine.
The CoroutineScope
is defining the lifecycle of all the asynchronous/suspending code called by this Coroutine, directly or indirectly (called by a suspend fun
that was called by the Coroutine, or called by yet another launch
or async
that was called by the Coroutine, etc).
When the CoroutineScope
of the Coroutine is cancelled, all the asynchronous/suspending code called by this Coroutine is cancelled. Also, only when all the ((great) (great) grand) children, i.e. child-`Job`s, have finished (just stopped or cancelled, or had an error), only then a Coroutine can end as well.
In other words, and worded with a bit of liberty;
a Coroutine, launched in a CoroutineScope and all its ((great) (great) grand) child-Jobs (running in the same scope) live and die togher.
This all is part of Coroutine's Structured Concurrency, which makes it easier to manage asynchronous/suspending code.