Stanley Gomes
05/18/2021, 3:49 AMviewmodelScope.launch {
val deferredFetchAndPersist = async {
some suspend fun()
}
val deferredTimer = async {
delay(3000)
}
awaitAll(deferredFetchAndPersist, deferredTimer)
hideImageView()
}
This works but I feel it's a little too verbose. Is there a better way I can achieve this?
ThanksErik
05/18/2021, 5:08 AMStanley Gomes
05/18/2021, 5:21 AMuli
05/18/2021, 7:34 AMcoroutineScope { … }
and use launch
instead of `async`and awaitAll
viewmodelScope.launch {
coroutineScope {
launch {
fetchAndPersist()
}
launch {
delay(3000)
}
}
hideImageView()
}
joinAll
the `Job`s returned from launch
instead of calling `awaitAll`on the `Deferred<Unit>`sbezrukov
05/18/2021, 9:17 AMdelay(3000 - measureTimeMillis {
yourSuspendFun()
})
hideImageView()
(delay works fine with negative arguments, so everything should work regardless if yourSuspendFun faster/slower than 3s)Erik
05/18/2021, 1:52 PMval actualMillis = measureTimeMillis { yourSuspendFun() }
val minimumMillis = 3000
delay(minimumMillis - actualMillis)
hideImageView()
Stanley Gomes
05/18/2021, 3:23 PM