https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

jeff

07/27/2021, 3:02 PM
I read today that
kotlinx.coroutines.delay
on Android uses
Handler.postDelayed
under the hood. That surprised me -- is it true? For Dispatcher.Main only, or all dispatchers?
z

Zach Klippenstein (he/him) [MOD]

07/27/2021, 3:10 PM
Of the official
Dispatchers.*
dispatchers, it’s only for
Dispatchers.Main
, and only on Android. It’s also for any custom dispatchers derived from `Handler`s (thanks @Adam Powell for the correction). On jvm, delay uses the AWT/Swing event loop. The whole point of
Dispatchers.Main
is that it delegates to the current platform’s main thread event loop.
☝️ 1
m

mateusz.kwiecinski

07/27/2021, 3:13 PM
I can only add related source code: https://github.com/Kotlin/kotlinx.coroutines/blob/master/ui/kotlinx-coroutines-android/src/HandlerDispatcher.kt#L55 and its documentation 🙂 I consider kotlin and coroutines implementation well-written and it usually clears all doubts one may have. I hope you’ll find it useful as well 🙂
j

jeff

07/27/2021, 3:14 PM
Thanks!
a

Adam Powell

07/27/2021, 3:17 PM
It's for any dispatcher derived from a Handler, anyway.
6 Views