Skip to content

Instantly share code, notes, and snippets.

@anandwana001
Created October 4, 2025 03:09
Show Gist options
  • Select an option

  • Save anandwana001/0dbb0b7d1b2f2c15f3d14e167e1097a3 to your computer and use it in GitHub Desktop.

Select an option

Save anandwana001/0dbb0b7d1b2f2c15f3d14e167e1097a3 to your computer and use it in GitHub Desktop.
// ❌ Wrong: CPU-intensive work on IO dispatcher
suspend fun processData(data: List<Int>) = withContext(Dispatchers.IO) {
data.map { it * it * it * it } // CPU-intensive
}
// ✅ Correct: CPU-intensive work on Default dispatcher
suspend fun processData(data: List<Int>) = withContext(Dispatchers.Default) {
data.map { it * it * it * it }
}
// ❌ Wrong: Network call on Default dispatcher
suspend fun fetchData() = withContext(Dispatchers.Default) {
// Network call - will block a CPU thread!
}
// ✅ Correct: Network call on IO dispatcher
suspend fun fetchData() = withContext(Dispatchers.IO) {
// Network call - uses IO thread that can wait
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment