Android Room Compilation错误暂停功能@Transaction

时间:2019-06-03 09:30:23

标签: android-room kotlin-coroutines

我在Room @Transaction中的暂停功能遇到问题

版本:

room_version = "2.1.0-alpha04"
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-coroutines:$room_version"
kapt "androidx.room:room-compiler:$room_version"

我也尝试使用2.1.0-rc01。

科特琳:1.3

这是我的DAO界面:

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(sifrBl: SifrBl)

@Query("DELETE FROM sifrbl")
suspend fun deleteAll()

@Transaction
suspend fun setSifrBl(sifrBl: SifrBl){
    deleteAll()
    insert(sifrBl)
}

在编译时出现以下错误:

Method annotated with @Transaction must not return deferred/async return type androidx.lifecycle.LiveData. Since transactions are thread confined and Room cannot guarantee that all queries in the method implementation are performed on the same thread, only synchronous @Transaction implemented methods are allowed. If a transaction is started and a change of thread is done and waited upon then a database deadlock can occur if the additional thread attempts to perform a query. This restrictions prevents such situation from occurring.

我指的是this

如果我删除了所有功能中的暂停,那么它将起作用。

我们将不胜感激:)

3 个答案:

答案 0 :(得分:0)

查看注解处理器的源代码后,我可以说给定的代码不可能产生所述错误,因为仅当方法返回LiveData<*>对象时,该错误才会触发。 (reference

作为参考,任何以某种方式返回@Transaction对象,RxJava对象或Guava的LiveData对象的ListenableFuture方法都将触发此异常,因为它们可能会导致从方法。请注意,这是一个黑名单,任何推迟执行的对象都同样有问题,但不会触发错误。

答案 1 :(得分:0)

依赖项存在问题。

room_version = "2.1.0-rc01"

    //Room
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"

// optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"

// Test helpers
testImplementation "androidx.room:room-testing:$room_version"

现在可以了。

答案 2 :(得分:0)

接受的答案对我不起作用。我不得不打开该方法,然后它起作用了。检查生成的类,我可以看到为什么有必要将方法打开,因为该方法在生成的类中已被覆盖。