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

Maciek

07/21/2020, 2:33 PM
Is it normal that coroutine takes almost 50ms to run the code in the
launch
block? No context switching, same thread. Simple
MainScope().launch {}
. Is there a way to run it sequentially or at least with smaller delays?
s

sikri

07/21/2020, 2:43 PM
Copy code
printTime() // 0 secs passed
launch {
  printTime() // 5 secs passed
}
sleep(5)
if you don’t do context switching and, as you said, same thread, it is okay for coroutine to move your code block to the end of the block stack, so it won’t run, until previous block is finished
m

Maciek

07/21/2020, 5:48 PM
ooh, that makes sense, thanks!
l

Lukas Lechner

07/29/2020, 2:51 PM
If thats on Android then launching a coroutine on the main thread is like posting a runnable to the message queue of the main thread and this can take some time if it is able to process your runnable. I think there is
Dispatchers.Main.immediate
that executes your launched coroutine immediately.
👍 2
m

Maciek

07/31/2020, 6:58 PM
awesome, that's what I was looking for, thanks!
4 Views