asOutcomeReceiver
Returns an OutcomeReceiver that will resume this Continuation when an outcome is reported.
Useful for writing suspend
bindings to async Android platform methods that accept OutcomeReceiver:
public suspend fun FancinessManager.query(
query: FancinessManager.Query
): FancinessManager.QueryResult = suspendCancellableCoroutine<QueryResult> { continuation ->
// Any Android API that supports cancellation should be configured to propagate
// coroutine cancellation as follows:
val canceller = CancellationSignal()
continuation.invokeOnCancellation { canceller.cancel() }
// Invoke the FancinessManager#queryAsync method as follows:
queryAsync(
query,
canceller,
// Use a direct executor to avoid extra dispatch. Resuming the continuation will
// handle getting to the right thread or pool via the ContinuationInterceptor.
Runnable::run,
continuation.asOutcomeReceiver()
)
}
Content copied to clipboard