I'm wrapping my head around the coroutine concept in Kotlin/Android. So, as I do not want to use Timertask, Handler with a post delayed I want to use coroutines to execute an async coroutine after a certain delay. I have the following semi-code:
launch(UI) {
val result = async(CommonPool) {
delay(30000)
executeMethodAfterDelay()
}
result.await()
}
The problem with this is that actually in the async is both method's (delay and executeMethodAfterDelay) are executed at the same time. While I was expecting that first 30 seconds of delay would be introduced before executeMethodAfterDelay() would be fired. So my question is, how can I achieve this?
You can simplify your code to just this:
launch(UI) {
delay(30000)
executeMethodAfterDelay()
}
If you specifically want your method to run outside the GUI thread, then write
launch(CommonPool) {
delay(30000)
executeMethodAfterDelay()
}
More typically you'll want to execute a long-running method off the GUI thread and then apply its result to the GUI. This is how it's done:
launch(UI) {
delay(30000)
val result = withContext(CommonPool) {
executeMethodAfterDelay()
}
updateGuiWith(result)
}
Note you don't need async-await
in any scenario.
As for your specific report about delay
running concurrently with executeMethodAfterDelay
, this is not actually happening. Here's some self-contained code you can try out:
import kotlinx.coroutines.experimental.*
fun main(args: Array<String>) {
runBlocking {
val deferred = async(CommonPool) {
println("Start the delay")
delay(3000)
println("Execute the method")
executeMethodAfterDelay()
}
val result = deferred.await()
println("Method's result: $result")
}
}
fun executeMethodAfterDelay() = "method complete"
This will be the program's behavior:
Start the delay
... three seconds pass ...
Execute the method
Method's result: method complete
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With