This message was deleted.
# coroutines
s
This message was deleted.
l
A scope has a context
c
Yeah, but why do we need to define a scope and a context, I'm trying to make a clear distinction about why each is important and what it's actual role is
l
A
CoroutineScope
is there to provide a default
CoroutineContext
to use for coroutine builders like
launch
. Also, a scope makes concurrency structured, making leaks harder to do.
f
Scope: every coroutine needs to be created on a scope. A cancellation or unhandled exception on a coroutine influences other coroutines in the same scope. It is also possible to synchronise with the completion of all coroutines on a scope. Context: is an immutable set of context elements that is always accessible by a coroutine (even if hosted on different threads) during its lifetime (similar to Thread Local Storage for "classical" threads). Some of this context elements also control some of the coroutine behaviour, namely thread dispatching. Makes sense?
👍 2
m
All coroutine builders are launched within a particular scope. The scope itself wraps a context that holds things like the coroutine name, dispatcher, uncaught exception handler and job