kotlin coroutine_Kotlin coroutine and delay
kotlin coroutine
Whenever I suspect that there’s a timing conflict causing a problem with rendering and directives, I usually opt for a JavaScript setTimeout
with a delay. The setTimeout
code never makes it to production, but it does help me to understand if my code is the problem or if there’s a timing conflict.
Whenever I suspect a timing violation causing issues with rendering and directives, I usually opt for JavaScript with a delay setTimeout
. The setTimeout
code will never make it to production, but it does help me understand if it’s an issue with my code or a timing conflict.
In working with Kotlin on Android, I’ve needed to employ the same technique. Kotlin obviously doesn’t have a setTimeout
, but it does have coroutines to achieve approximately the same effect.
p>
I need to employ the same technique when using it with Kotlin on Android. Kotlin obviously doesn’t have setTimeout
, but it does have coroutines to achieve roughly the same effect.
To run an async coroutine with delay, you can use the following Kotlin code:
To run an asynchronous coroutine lazily, you can use the following Kotlin code:
// Create an async coroutine
GlobalScope. launch {
delay(1000)
// Execute code to test functionality
}
The coroutine becomes async and the delay can be whatever amount of milliseconds you’d like!
The coroutine becomes asynchronous, the delay may be as many milliseconds as you want!
Translated from: https://davidwalsh.name/kotlin-coroutines
kotlin coroutine