【发布时间】:2018-07-19 13:14:58
【问题描述】:
我正在尝试使用kotlin协程通过here描述的方法访问房间数据库,添加插件和依赖,并在gradle中启用kotlin协程。
在 gradle 文件中:
kotlin {
experimental {
coroutines 'enable'
}
}
dependencies { implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.21" ...}
所以我为 dao 类中的所有方法添加了suspend 关键字,如下所示:
道类
@Query("select * from myevent")
suspend fun all(): List<MyEvent>
@Delete
suspend fun deleteEvent(event: MyEvent)
...
构建,然后得到这些错误
错误
e: C:\Users\projectpath\app\build\tmp\kapt3\stubs\debug\com\robyn\myapp\data\source\local\EventsDao.java:39: error: Deletion methods must either return void or return int (the number of deleted rows).
public abstract java.lang.Object deleteEventById(@org.jetbrains.annotations.NotNull()
^
e: C:\Users\projectpath\app\build\tmp\kapt3\stubs\debug\com\robyn\myapp\data\source\local\EventsDao.java:41: error: Query method parameters should either be a type that can be converted into a database column or a List / Array that contains such type. You can consider adding a Type Adapter for this.
kotlin.coroutines.experimental.Continuation<? super kotlin.Unit> p1);
错误链接导航到自动生成 dao 类。此类中生成的方法现在每个都有一个此类型的附加参数 Continuation ,如下所示:
自动生成的 dao 类
@org.jetbrains.annotations.Nullable()
@android.arch.persistence.room.Delete()
public abstract java.lang.Object deleteAllEvents(@org.jetbrains.annotations.NotNull() // error indicates at this line
java.util.List<com.robyn.myapp.data.MyEvent> events, @org.jetbrains.annotations.NotNull()
kotlin.coroutines.experimental.Continuation<? super kotlin.Unit> p1); // error indicates at this line
...
我尝试删除生成的 dao 类并重建以重新生成它,但仍然出现这些错误。我考虑不使用lauch{} 方法而是使用suspend 关键字,因为代码中有很多地方可以查询db。
我该如何解决这个问题?
【问题讨论】:
标签: android kotlin coroutine android-room