在 2.1 Room 中添加了对协程的支持,但我无法让它工作。它应该像添加依赖项一样简单,但不知何故我忽略了一些东西。
在我的 build.gradle 中,我获得了协程、房间和房间协程的依赖项
dependencies {
def room_version = "2.2.0-beta01"
// Room components
implementation "android.arch.persistence.room:runtime:$room_version"
kapt "android.arch.persistence.room:compiler:$room_version"
implementation "androidx.room:room-ktx:$room_version"
androidTestImplementation "android.arch.persistence.room:testing:$room_version"
def coroutines_version = "1.1.1"
// Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
}
我已经尝试过重新同步 gradle、清理和重建项目。
在我的 Doa 中,我有如下方法
@Dao
interface PlanDao {
@Insert
suspend fun insertVerPlan(verPlan: SqlVerPlan)
}
当尝试构建项目时,Room 不知道如何处理挂起函数,并发生以下错误:
error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
kotlin.coroutines.Continuation<? super kotlin.Unit> p1);
^
error: Methods annotated with @Insert can return either void, long, Long, long[], Long[] or List<Long>.
public abstract java.lang.Object insertVerPlan(@org.jetbrains.annotations.NotNull()
^
我真的不知道我错过了什么,而且自从新的 Room 版本以来我找不到任何有同样问题的人。
最佳答案
您正在混合不同版本的房间库。
android.arch.persistence.room:runtime
应该是 androidx.room:room-runtime
android.arch.persistence.room:compiler
应该是 androidx.room:room-compiler
android.arch.persistence.room:testing
应该是 androidx.room:room-testing
由于您使用的是编译器的旧坐标,因此它不知道挂起
支持。
关于android - 将 kotlin 协程与 room 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57739890/