Skip to content
DeveloperMemos

Using Dispatcher.Main and Dispatcher.IO together(in an Android App)

Kotlin Coroutines, Dispatcher.Main, Dispatcher.IO, Android3 min read

Dispatcher.Main and Dispatcher.IO are two important components of the Kotlin Coroutines library that can be used in Android app development to manage threads and asynchronous tasks. In this article, we will look at how to use these two classes in an Android app and how they can help you write more efficient, responsive, and maintainable code.

What are Kotlin Coroutines?

Kotlin Coroutines is a library that helps you write asynchronous code in a more concise and easier to read way. It was introduced in Kotlin 1.3 and has since become a popular tool for Android developers. Coroutines allow you to write code that runs asynchronously, without having to worry about the complexities of thread management.

Coroutines are lightweight threads that can be paused and resumed at any point. They are very efficient and use less memory and CPU resources compared to traditional threads. They also provide a safer and more structured way to handle asynchronous tasks, as they can be easily cancelled, resumed, and controlled.

Dispatcher.Main

Dispatcher.Main is a special coroutine dispatcher that is designed to be used for UI-related tasks. It is provided by the kotlinx.coroutines library and is the equivalent of the Android MainThreadExecutor.

When you launch a coroutine using Dispatcher.Main, the coroutine will be executed on the main thread of the app. This is useful when you want to update the UI or perform any other operation that needs to be run on the main thread.

Here's an example of how to use Dispatcher.Main to update the text of a TextView:

1import kotlinx.coroutines.*
2
3// ...
4
5GlobalScope.launch(Dispatchers.Main) {
6 textView.text = "Hello, World!"
7}

In the example above, we are using the launch function of the GlobalScope to start a new coroutine. The Dispatchers.Main parameter specifies that the coroutine should be executed on the main thread.

Dispatcher.IO

Dispatcher.IO is another special coroutine dispatcher that is designed to be used for blocking IO-bound tasks. It is also provided by the kotlinx.coroutines library and is the equivalent of the Android Executors.newSingleThreadExecutor().

When you launch a coroutine using Dispatcher.IO, the coroutine will be executed on a background thread that is dedicated to IO operations. This is useful when you need to perform a blocking IO operation, such as reading from a file or making a network request.

Here's an example of how to use Dispatcher.IO to make a network request:

1import kotlinx.coroutines.*
2import okhttp3.*
3
4// ...
5
6GlobalScope.launch(Dispatchers.IO) {
7 val client = OkHttpClient()
8 val request = Request.Builder()
9 .url("https://www.example.com")
10 .build()
11 val response = client.newCall(request).execute()
12 println(response.body()?.string())
13}

In the example above, we are using the launch function of the GlobalScope to start a new coroutine. The Dispatchers.IO parameter specifies that the coroutine should be executed on a background thread dedicated to IO operations. We are using the OkHttp library to make a network request and print the response body to the console.

It's important to note that Dispatcher.IO is designed for blocking IO operations. If you need to perform a non-blocking IO operation, you should use a different coroutine dispatcher, such as Dispatchers.Default.

Using Dispatcher.Main and Dispatcher.IO in Android

In an Android app, you can use Dispatcher.Main and Dispatcher.IO to perform tasks on the main thread and background threads, respectively. This can help you write more efficient and responsive code, as you can offload heavy or blocking tasks to background threads and update the UI on the main thread.

Here's an example of how to use Dispatcher.Main and Dispatcher.IO to load an image from the network and display it in an ImageView:

1import android.widget.ImageView
2import kotlinx.coroutines.*
3import okhttp3.*
4
5// ...
6
7fun loadImage(imageView: ImageView, url: String) {
8 GlobalScope.launch(Dispatchers.IO) {
9 val client = OkHttpClient()
10 val request = Request.Builder()
11 .url(url)
12 .build()
13 val response = client.newCall(request).execute()
14 val imageData = response.body()?.bytes()
15
16 withContext(Dispatchers.Main) {
17 imageView.setImageBitmap(BitmapFactory.decodeByteArray(imageData, 0, imageData.size))
18 }
19 }
20}

In the example above, we are using Dispatcher.IO to perform the network request and load the image data in the background. We are then using Dispatcher.Main and the withContext function to update the ImageView on the main thread.

It's important to note that you should not perform heavy or blocking tasks on the main thread, as this can cause the app to freeze or become unresponsive. By using Dispatcher.Main and Dispatcher.IO, you can ensure that your app remains responsive and efficient.

Conclusion

In this article, we looked at how to use Dispatcher.Main and Dispatcher.IO in an Android app. Dispatcher.Main allows you to perform tasks on the main thread, while Dispatcher.IO allows you to perform blocking IO operations on a background thread. By using these two classes, you can write more efficient and responsive code for your Android app.