Use Yield in Heavy Functions
It is good practice to use yield in suspending functions between blocks of non-suspended CPU-intensive or time-intensive operations. This function suspends and immediately resumes the coroutine, thus it supports cancellation. Calling yield also allows redispatching, thanks to which one process will not starve other processes. - Kotlin Coroutines Deep Dive
suspend fun cpuIntensiveOperations() =
withContext(Dispatchers.Default) {
cpuIntensiveOperation1()
yield()
cpuIntensiveOperation2()
yield()
cpuIntensiveOperation3()
}
Backlinks