我正在使用Room架构组件来实现持久性.我创建了通用DAO接口以避免样板代码. Room Pro Tips
但是我的代码没有编译说"Error:(21,19)error:参数的类型必须是用@Entity或它的集合/数组注释的类." 对于通用类T.
interface BaseDao<T> {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(T... entity);
@Update
void update(T entity);
@Delete
void delete(T entity);
}
@Dao
public abstract class ReasonDao implements BaseDao<ReasonDao> {
@Query("SELECT * from Reason")
abstract public List<Reason> getReasons();
}
Run Code Online (Sandbox Code Playgroud)
这里有什么我想念的吗?它是这样工作在这里
我正在尝试使用kotlin协同程序通过此处描述的方法访问room数据库,添加插件和依赖项,并在gradle中启用kotlin协同程序.
在gradle文件中:
kotlin {
experimental {
coroutines 'enable'
}
}
dependencies { implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.21" ...}
Run Code Online (Sandbox Code Playgroud)
所以我suspend为dao类中的所有方法添加了关键字,如下所示:
道类
@Query("select * from myevent")
suspend fun all(): List<MyEvent>
@Delete
suspend fun deleteEvent(event: MyEvent)
...
Run Code Online (Sandbox Code Playgroud)
并构建,然后得到这些错误
错误
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 …