RunnableKt

Added in 1.8.0

public final class RunnableKt


Summary

Public methods

static final @NonNull Runnable

Returns a Runnable that will resume this Continuation when an operation completes and the returned Runnable's Runnable.run method is called.

Public methods

asRunnable

public static final @NonNull Runnable asRunnable(@NonNull Continuation<Unit> receiver)

Returns a Runnable that will resume this Continuation when an operation completes and the returned Runnable's Runnable.run method is called.

Useful for writing suspend bindings to async Jetpack library methods that accept Runnable as a completion callback for a one-time operation:

public suspend fun FancinessManager.setFanciness(
fanciness: Float
): Unit = suspendCancellableCoroutine<Unit> { 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#setFanciness method as follows:
queryAsync(
fanciness,
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.asRunnable()
)
}