Coroutines Highlights - CoroutineContext
Júlio Siqueira
Senior Android Engineer | 8+ years | Mobile | Kotlin Software Developer | Java
The CoroutineContext is a set of various elements and it’s passed in?the constructor of CoroutinesScope or CoroutineBuilders.
Notice that even you don’t manually pass some of these elements, Kotlin is going to create some them by default.
?The coroutine context is formed by the following elements:
Job
A background job. Conceptually, a job is a cancellable thing with a life-cycle that culminates in its completion.
Jobs can be arranged into parent-child hierarchies where cancellation of a parent leads to immediate cancellation of all its children recursively. Failure of a child with an exception other than CancellationException immediately cancels its parent and, consequently, all its other children. This behavior can be customized using SupervisorJob.
Dispatcher
A coroutine dispatcher determines which thread or threads the coroutine will use for it’s execution. It determines the context in which the coroutine code runs.
Common Dispatcher’s include: Dispatchers.Main, Dispatchers.Default,?Dispatchers.IO and Dispatchers.Unconfined
ExceptionHandler
Handles the uncaught exceptions. In some cases, can replace try-catch blocks
Coroutine Name
User-specified name of coroutine. This name is used in debugging mode.?
References
Job?