OutcomeReceiverKt

Added in 1.8.0

public final class OutcomeReceiverKt


Summary

Public methods

static final @NonNull OutcomeReceiver<@NonNull R, @NonNull E>
@RequiresApi(value = 31)
<R extends Object, E extends Throwable> asOutcomeReceiver(
    @NonNull Continuation<@NonNull R> receiver
)

Returns an OutcomeReceiver that will resume this Continuation when an outcome is reported.

Public methods

asOutcomeReceiver

@RequiresApi(value = 31)
public static final @NonNull OutcomeReceiver<@NonNull R, @NonNull E> <R extends Object, E extends Throwable> asOutcomeReceiver(
    @NonNull Continuation<@NonNull R> receiver
)

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()
)
}