More on Constructing a Coroutine Scope
Explore how to construct coroutine scopes effectively in typical Android and backend scenarios. Understand the use of viewModelScope, lifecycleScope, custom dispatchers, SupervisorJob, and CoroutineExceptionHandler to build robust coroutine scopes suited for real-life projects and additional operations.
viewModelScope and lifecycleScope
In modern Android applications, instead of defining our own scope, we can also use viewModelScope or lifecycleScope. They work almost identically to what we’ve just constructed. They use Dispatchers.Main and SupervisorJob, and they cancel the job when the view model or lifecycle owner gets destroyed.
Using viewModelScope and lifecycleScope is convenient and is recommended if we do not need any unique context as a part of our scope (like ...