# Coroutines and Dispatchers

DevFeed: [Coroutines and Dispatchers](<https://devfeed.tech/articles/coroutines-and-dispatchers-22871.md>)

Original publisher: [Read original article](<https://medium.com/mindorks/coroutines-and-dispatchers-b559094b828e?source=rss----f1a763fc7443---4>)

Author: Bigyan Thapa

Published: 2024-10-16T06:48:00Z

Content type: tutorial

Language: en

Sources: [Mindorks - Medium](<https://devfeed.tech/sources/mindorks-medium.md>)

Topics: [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [android-development](<https://devfeed.tech/topics/android-development.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Development](<https://devfeed.tech/topics/development.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-development](<https://devfeed.tech/tags/android-development.md>), [androiddev](<https://devfeed.tech/tags/androiddev.md>), [best-practices](<https://devfeed.tech/tags/best-practices.md>), [code-quality](<https://devfeed.tech/tags/code-quality.md>), [coroutine](<https://devfeed.tech/tags/coroutine.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [dispatcher](<https://devfeed.tech/tags/dispatcher.md>), [extension](<https://devfeed.tech/tags/extension.md>), [extension-function](<https://devfeed.tech/tags/extension-function.md>), [io](<https://devfeed.tech/tags/io.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [main-thread](<https://devfeed.tech/tags/main-thread.md>)

## AI overview

This tutorial explains how Android ViewModel coroutines use viewModelScope and how to select Dispatchers.IO, Dispatchers.Default, or Dispatchers.Main for I/O, CPU-intensive, and UI work. It also presents ViewModel extension functions and an overloaded function to standardize coroutine launching.

## Source excerpt

In Android development, coroutines are commonly launched from ViewModel classes using viewModelScope. The typical usage looks like this: viewModelScope.launch { // ... implementation } In Android development, coroutines are commonly launched from ViewModel classes using viewModelScope. The typical usage looks like this: viewModelScope.launch { // ... implementation } By default, if a dispatcher is not specified, any coroutine launched in viewModelScope will run on the main thread. However, in most use cases, we launch these coroutines to perform background tasks, such as -- making API calls, or database operations. To ensure these tasks run in the background thread, it is essential to specify an appropriate dispatcher: I/O tasks should use Dispatchers.IO CPU-intensive tasks should use Dispatchers.Default UI updates should use Dispatchers.Main For example, to run an I/O task, we can specify the dispatcher like this: viewModelScope.launch(Dispatchers.IO) { // ...implementation }Optimizing Coroutine Launching To streamline coroutine launching, we can create extension functions on ViewModel that automatically use the specified dispatcher by default. Step 1: Create Extension Functions We can define extension functions for different dispatchers: kotlin fun ViewModel.launchIO(block: suspend CoroutineScope.() -> Unit) { viewModelScope.launch(Dispatchers.IO, block = block) } fun ViewModel.launchDefault(block: suspend CoroutineScope.() -> Unit) { viewModelScope.launch(Dispatchers.Default, block = block) } fun ViewModel.launchMain(block: suspend CoroutineScope.() -> Unit) { viewModelScope.launch(Dispatchers.Main, block = block) }Step 2: Use the Extension Functions You can now use these extension functions in your ViewModel as follows: class MyViewModel : ViewModel() { fun fetchData() { launchIO { // ... implementation } } } Benefits Consistency: These extension functions maintain consistent coroutine usage for specific tasks. Readability: The function names clearly indicate their p