ConsumerKt

Added in 1.8.0

public final class ConsumerKt


Summary

Public methods

static final @NonNull Consumer<@NonNull T>
@RequiresApi(value = 24)
<T extends Object> asConsumer(@NonNull Continuation<@NonNull T> receiver)

Returns a java.util.function.Consumer that will resume this Continuation when the result of an operation is accepted.

Public methods

asConsumer

@RequiresApi(value = 24)
public static final @NonNull Consumer<@NonNull T> <T extends Object> asConsumer(@NonNull Continuation<@NonNull T> receiver)

Returns a java.util.function.Consumer that will resume this Continuation when the result of an operation is accepted.

Useful for writing suspend bindings to async methods that accept java.util.function.Consumer as a result callback for a one-time operation:

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