Coroutines Highlights - CoroutineContext

Coroutines Highlights - CoroutineContext


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

Coroutine context and dispatchers | Kotlin Documentation

Exploring CoroutineContext. Hey there! Welcome to my blog, where… | by Tanya Arora | ProAndroidDev?

Job?

CoroutineName?

Structured Concurrency with Kotlin coroutines | KINTO Tech Blog | キントテックブログ

要查看或添加评论,请登录

Júlio Siqueira的更多文章

  • Coroutines Highlights - Scope

    Coroutines Highlights - Scope

    1 - A CoroutineScope is a container for coroutines and coroutines context. It keeps track of any coroutine created…

社区洞察

其他会员也浏览了