https://kotlinlang.org logo
Title
c

codebijan

12/27/2019, 10:03 PM
Anyone tried fetching firestore documents and have reference as to how to await for a pending fetch?
x

Xavier Hanin

01/06/2020, 11:15 AM
@codebijan coming a bit late, but I use extension function for that:
/**
 * Converts this api future to the instance of [CompletableFuture].
 * Inspired by Kotlin coroutines Deferred<T>.asCompletableFuture().
 */
fun <T> ApiFuture<T>.asCompletableFuture(): CompletableFuture<T> {
    val future = CompletableFuture<T>()
    addListener({
        try {
            future.complete(get())
        } catch (t: CancellationException) {
            future.cancel(true)
        } catch (t: InterruptedException) {
            future.cancel(true)
        } catch (t: ExecutionException) {
            future.completeExceptionally(t.cause)
        } catch (t: Throwable) {
            future.completeExceptionally(t)
        }
    }, {r:Runnable -> r.run()})
    return future
}

suspend fun <T> ApiFuture<T>.await(): T = asCompletableFuture().await()
g

Gabriel Feo

01/15/2020, 8:52 AM
There's also a coroutines integration library for Google's Task API