takahirom
09/18/2018, 2:04 AMkotlin
api.fetchPersons()
.subscribeOn(scheduler)
.subscribe(
{ persons ->
showPersons(persons)
},
{ e ->
showErrorDialog(e)
})
I enclosed with try catch to do the same on kotlin coroutines as follows.
But in this case CancellationException did not go to parents job, so I thought that it would be in an invalid state. Even if CancellationException is caught by the user, the outer block is not just canceled, is there no problem? Is it fine just like this?
kotlin
launch {
try {
val persons = api.fetchPersons() // suspend function
showPersons(persons)
} catch (e: Throwable) {
showErrorDialog(e)
}
}
gildor
09/18/2018, 2:49 AMgildor
09/18/2018, 2:49 AMtakahirom
09/18/2018, 3:29 AMCanceled child does not cancel parentI had a misunderstanding. I still do not know if it is safe to catch CancelationException. Do I need to take this form?
launch {
try {
val persons = api.fetchPersons() // suspend function
showPersons(persons)
} catch (cancellationException: CancellationException) {
throw cancellationException
} catch (e: Throwable) {
showErrorDialog(e)
}
}
gildor
09/18/2018, 4:41 AMtakahirom
09/19/2018, 2:15 AMcancels coroutine
mean throwing CancelationException?
val job = launch {
try {
delay(Int.MAX_VALUE) // cancel here
} catch (e: CancellationException) {
e.printStackTrace()
}
}
job.cancel()
gildor
09/19/2018, 3:10 AM.cancel()
) and you need it only if you somehow want handle this casetakahirom
09/19/2018, 6:05 AMgildor
09/19/2018, 8:09 AM