Conclusion

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In this lesson, you learned:

  • 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
Download course materials from Github
Previous: Demo Next: Understand Scope and Dispatchers