What if your app never froze again, no matter how much data it handled?
Why Dispatchers.Main, IO, Default behavior in Kotlin? - Purpose & Use Cases
Imagine you are building an app that fetches data from the internet and updates the screen. If you try to do everything on the same thread, the app freezes and becomes unresponsive.
Doing all tasks manually on one thread is slow and risky. The app can freeze, crash, or behave unpredictably because heavy work blocks the screen updates.
Using Dispatchers like Main, IO, and Default lets you run tasks on the right threads automatically. This keeps the app smooth and fast without freezing.
fun fetchData() {
val data = fetchFromNetwork() // blocks UI
updateUI(data)
}fun fetchData() = CoroutineScope(Dispatchers.IO).launch {
val data = fetchFromNetwork()
withContext(Dispatchers.Main) {
updateUI(data)
}
}You can write simple code that runs heavy tasks in the background and updates the UI smoothly without freezing.
When you scroll a social media feed, images load in the background (IO dispatcher) while the screen stays responsive (Main dispatcher).
Manual threading causes app freezes and bugs.
Dispatchers manage where code runs for best performance.
Main is for UI, IO for network/disk, Default for CPU work.