Coroutines Highlights - Scope


1 - A CoroutineScope is a container for coroutines and coroutines context. It keeps track of any coroutine created within the scope, forming a hierarchy.


Running launch and async from coroutine scope


2 - launch and async builders are actually extensions of the CoroutineScope interface. So whenever we want to launch a coroutine, we need to start them in some scope


async bulder is a extension function from CoroutineScope


3 - Scope defines the? lifetime of the coroutine. When a scope is canceled, all coroutines launched within that scope are automatically canceled.?

For example, if we run the code below from an Android Activity and close the activity before the coroutine ends, will throw a CancellationException


using lifecycleScope to keep tracking of coroutine during Activity Lifecycle



4 - The coroutineScope reference can be used to cancel the coroutines manually or check if it is still active



References

Coroutines basics | Kotlin Documentation?

Difference Between Coroutine Scope and Coroutine Context | Baeldung on Kotlin

Exploring Structured Concurrency with Kotlin Coroutines | by Rizwanul Haque | Medium?

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

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

Júlio Siqueira的更多文章

  • Coroutines Highlights - CoroutineContext

    Coroutines Highlights - CoroutineContext

    The CoroutineContext is a set of various elements and it’s passed in the constructor of CoroutinesScope or…

社区洞察

其他会员也浏览了