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

JoakimForslund

09/27/2019, 10:06 AM
Question: Should not
runBlocking(Dispatchers.Main) {}
be the same as running
Handler(Looper.getMainLooper()).post{}
on android?
g

gildor

09/27/2019, 10:09 AM
Yes, it’s pretty close, Dispatchers.Main on Android uses Handler with main looper to dispatch coroutines UPD: just now realized that you use runBlocking, no, it’s not the same, because it’s blocking
j

JoakimForslund

09/27/2019, 10:13 AM
I'm curious, because it does not seem to translate well when trying to for instance, for a webview javascript bridge, force the call to run on main thread. I can run and force the thread (without issues)
Copy code
@JavascriptInterface
        override fun back() {
            Handler(Looper.getMainLooper()).post {
                this@PlatformSpecific.back()
            }
        }
But running this:
Copy code
@JavascriptInterface
        override fun back() {
            runBlocking(Dispatchers.Main) {
                this@PlatformSpecific.back()
            }
        }
Seems to lock the thread switch entirly
g

gildor

09/27/2019, 10:16 AM
Ohhh,
sorry, I just now got what you are doing
No, it;’s not the same
do not use runBlocking for this, it will block the thread
use
launch(Dispatchers.Main)
instead
j

JoakimForslund

09/27/2019, 10:18 AM
Oh yeah.. I'm a retard you are of course correct. I'm not trying to run a blocking action at all..
g

gildor

09/27/2019, 10:22 AM
Yeah, I just also was a bit confused by not-common combination of runBlocking and async handler in your question
2 Views