agrosner / DBFlow

A blazing fast, powerful, and very simple ORM android database library that writes database code for you.
MIT License
4.87k stars 598 forks source link

Can't use Kotlin Deferred result in a DAO with DBFlow Transaction #1649

Closed forntoh closed 3 years ago

forntoh commented 5 years ago

DBFlow Version: 5.0.0-alpha1 "com.github.agrosner.dbflow:coroutines:299a76343b"

I am unable to use Kotlin Coroutines in a DAO with DBFlow Transaction

The function .defer() can't be found

fun coroutineRetrieveUSD(): Deferred<MutableList<Currency>> =
          database.beginTransactionAsync {
              (select from Currency::class
                      where (Currency_Table.symbol eq "$")).queryList(it)
          }.defer()
forntoh commented 5 years ago

What about my Issue

agrosner commented 3 years ago

defer was added in a later commit.

you should either switch to newer version or use:

fun <R : Any?> Transaction.Builder<R>.defer(): Deferred<R> {
    val deferred = CompletableDeferred<R>()
    val transaction = success { _, result -> deferred.complete(result) }
            .error { _, throwable -> deferred.completeExceptionally(throwable) }
            .build()
    deferred.invokeOnCompletion {
        if (deferred.isCancelled) {
            transaction.cancel()
        }
    }
    transaction.execute()
    return deferred
}