Created
October 4, 2025 03:09
-
-
Save anandwana001/0dbb0b7d1b2f2c15f3d14e167e1097a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ❌ 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