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

Florian

08/20/2021, 5:21 PM
I read that coroutine delays are not precise. What will cause a
delay
to take longer than the specified time? And do you think this will work to get the actual passed time after a delay (for example for a countdown timer)?
Copy code
val timeBefore = SystemClock.elapsedRealtime()
delay(TICK_DELAY)
val timeAfter = SystemClock.elapsedRealtime()
val passedTime = timeAfter - timeBefore
l

louiscad

08/20/2021, 5:57 PM
measureTime { }
from the stdlib should suit your needs. If the Dispatcher, or the VM/machine is short on resources, as can be seen when your computer freezes, the actual delay will be longer than requested.
The computer/device going to sleep, or your app being paused can also lead to delay being longer than what requested.
f

Florian

08/20/2021, 7:44 PM
thank you for the info!
measureTime
seems useful!
28 Views