About scopes and dispatchers in Kotlin coroutines.
The scope of a coroutine is a basic way to control the lifetime of the coroutine.
Scopes form a parent-children hierarchy of coroutines. By default, if a coroutine fails it will cancel all its siblings and the parent. Unless there is a SupervisorJob in the context of some of the scopes in the hierarchy.
It’s better to use the predefined scopes bound to the lifecycle of the Android entities.
If you create your own scope, don’t forget to cancel it manually to avoid bugs.
The dispatcher is a mechanism that determines which thread the coroutine runs on. Choose the dispatcher according to the task you want to perform. Use the Default for CPU-bound tasks and the IO for network and disk requests. The Main dispatcher is for tasks that update the user interface. Keep in mind you can switch the dispatcher in the coroutine using the withContext() function.
In the next lesson, you’ll learn about returning values from coroutines.
See forum comments
This content was released on Jun 5 2024. The official support period is 6-months
from this date.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
Previous: Demo
Next: Understand Scope and Dispatchers
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.