Using Deferred<…> in Room DAO with Kotlin Coroutines

后端 未结 1 1304
说谎
说谎 2021-01-13 17:08

I\'m trying to use Coroutines with a Room database in an Android project. I\'ve found almost no documentation online, and I\'m wondering if it is possible to return De

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-13 18:00

    Your issue lies in that you're mixing the suspending converter and the Deferred converter. Use one or the other and your code will work as intended.

    • fun readMyObjectAsync(idMyObject: Int): Deferred - Best choice if you need to interface/be compatible with java code, since it doesn't require code transformations to actually function.
    • suspend fun readMyObjectAsync(idMyObject: Int): MyObject - If you're operating on pure kotlin this will allow better control through the context it is called in.

    0 讨论(0)
提交回复
热议问题