irus
11/15/2018, 3:01 PMwithTimeoutOrNull(1000) {
suspendCancellableCoroutine<Unit> { cont ->
cont.invokeOnCancellation {
cont.resumeWithException(CancellationException("Hello!")) // 1
cont.resumeWithException(RuntimeException("Hello!")) // 2
}
}
}
Can't find place in documenation where difference between (1) and (2) describedAlexandre
11/15/2018, 3:29 PMirus
11/15/2018, 3:42 PMirus
11/15/2018, 3:45 PMirus
11/15/2018, 3:51 PMCancellationException during cancellation? I trying to cancel coroutine and underlying future, and future itself has cancellation handler. So in this handler I normally resume with`CancellationException` (for case when future cancelled by http client).elizarov
11/15/2018, 4:02 PMinvokeOnCancellation. When invokedOnCancellation block is invoked, the suspended coroutine had been already resumed with CancellationException by coroutines machinery. invokeOnCancellation is there to notify you that this had happened, so that you can cancel some other work that had been performed.irus
11/15/2018, 4:06 PMsuspend fun HttpAsyncClient.execute(request: HttpUriRequest): HttpResponse {
return suspendCancellableCoroutine { cont: CancellableContinuation<HttpResponse> ->
val callback = object : FutureCallback<HttpResponse> {
override fun completed(result: HttpResponse) {
cont.resumeWith(Result.success(result))
}
override fun cancelled() {
cont.resumeWith(Result.failure(FutureCallbackCanceledException(request)))
}
override fun failed(ex: Exception) {
cont.resumeWith(Result.failure(ex))
}
}
val future = this.execute(request, callback)
cont.cancelFutureOnCancellation(future)
Unit
}
}
I have cancelled handler, and it's being called when invokeOnCancellation handler cancels futureelizarov
11/15/2018, 4:32 PMelizarov
11/15/2018, 4:33 PM