Mark
05/06/2020, 2:27 AMZach Klippenstein (he/him) [MOD]
05/06/2020, 2:37 AMContinuationInterceptor
that wraps your dispatcher.OnCancelListener
and cancel your Job when the coroutine is cancelled.Mark
05/06/2020, 2:51 AMZach Klippenstein (he/him) [MOD]
05/06/2020, 3:02 PMMark
05/07/2020, 2:06 AMContinuationInterceptor
also work?Zach Klippenstein (he/him) [MOD]
05/07/2020, 2:40 AMMark
05/07/2020, 2:43 AMZach Klippenstein (he/him) [MOD]
05/07/2020, 6:41 PMisActive/isCancelled
or calling ensureActive()
, regardless of suspension – e.g. tight loops might call ensureActive()
to cooperatively participate in cancellation.
2) Synchronously executes any cancellation logic registered via invokeOnCancellation
(on Jobs or on `CancellableContinuation`s) – this logic and not guaranteed to be on any particular thread.
3) Immediately cancel all child jobs recursively, as part of structured concurrency.
So there might be coroutines that don’t suspend at all, but still participate in cancellation. Cancellation and suspension are related, but cancellation involves a lot more than just resuming suspended coroutines, so cancellation should be propagated as quickly as possible.query
implementation could look something like:
override fun query(
uri: Uri,
projection: Array<String>,
queryArgs: Bundle,
cancellationSignal: CancellationSignal
): Cursor = runBlocking {
val job = coroutineContext[Job]!!
cancellationSignal.setOnCancelListener { job.cancel() }
// your code
}
Mark
05/08/2020, 2:45 AM