Ok, interesting concept but bow to cancel coroutine in the example presented? async { val image = loadImage(url).await() myUI.setImage(image) } And how to handle "failure" case when error happens while loading? What if we should distinguish between several type of errors e.g. server errors, no network connection, etc? Is there stack allocated for coroutines?
Coroutines does have its own stack for debugging purposes. The "async" function returns a "Deferred" object, which can be cancelled by calling the "cancel" function and passing through a Throwable as a parameter. Note that "await" doesn't need to be called in the "async" block. Below is a loose coupled Async/Await example: fun main(args: Array) { val deferred = (1..1_000_000).map { n -> async(CommonPool) { asyncWorkload(n) } } runBlocking { deferred.take(20).forEach { println("Num: ${it.await()}") } } } private suspend fun asyncWorkload(num: Int): Int { delay(1000) return num }
Can coroutines take executors like Completeable Future?
If I want a separate thread for computation?
I cannot find UI in the launch(), but i use runOnUiThread instead
Ok, interesting concept but bow to cancel coroutine in the example presented?
async {
val image = loadImage(url).await()
myUI.setImage(image)
}
And how to handle "failure" case when error happens while loading? What if we should distinguish between several type of errors e.g. server errors, no network connection, etc? Is there stack allocated for coroutines?
Coroutines does have its own stack for debugging purposes. The "async" function returns a "Deferred" object, which can be cancelled by calling the "cancel" function and passing through a Throwable as a parameter. Note that "await" doesn't need to be called in the "async" block. Below is a loose coupled Async/Await example:
fun main(args: Array) {
val deferred = (1..1_000_000).map { n ->
async(CommonPool) { asyncWorkload(n) }
}
runBlocking {
deferred.take(20).forEach { println("Num: ${it.await()}") }
}
}
private suspend fun asyncWorkload(num: Int): Int {
delay(1000)
return num
}
See 24:05 about exception handling. You can use regular try/catch.
isn't it answered at 33:50?
Thank you for great lecture! Would you be so kind and publish presentation slides as well?
Thanks for publishing this :) One small advice: its really hard to listen to it due to a hard accent of a presenter :(
Trust me it's not because of accent, she's just hard to listen at any language
Милаха