i did understand continuation , context but unable...
# announcements
c
i did understand continuation , context but unable to understand these
co-routine scopes
for coroutines can someone explain me in
layman
terms. builders are used to create coroutines dispatchers to alter context.
a
CoroutineContext
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
.
If you haven't read https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/ before, it's worth a read. It's full of ideas that were part of the zeitgeist when kotlin's structured concurrency was being designed
The "nursery" concept explained there and the motivations for it have direct parallels in kotlin's
CoroutineScope
. The
async with nursery
pattern shown there is structurally the same as kotlin's
coroutineScope {}
blocks.
s
To bridge the gap between regular/blocking/non-suspending and suspending code, you'd need to create a Coroutine. That is where where you call
CoroutineScope.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.
❤️ 1