【问题标题】:Android Room Kotlin throws Delete Query errorAndroid Room Kotlin 抛出删除查询错误
【发布时间】:2021-11-25 00:34:23
【问题描述】:

我正在尝试使用 Android Room 2.3.0,但目前出现以下编译错误:

项目道:

error: Not sure how to handle query method's return type (java.lang.Object). DELETE query methods must either return void or int (the number of deleted rows).
    public abstract java.lang.Object deleteAllProjects(@org.jetbrains.annotations.NotNull()
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.Continuation<? super kotlin.Unit> continuation);
error: Unused parameter: continuation
    public abstract java.lang.Object deleteAllProjects(@org.jetbrains.annotations.NotNull()
error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
    kotlin.coroutines.Continuation<? super java.lang.Long> continuation);
error: Not sure how to handle insert method's return type.
    public abstract java.lang.Object insertProject(@org.jetbrains.annotations.NotNull()
error: Not sure how to handle delete method's return type. Currently the supported return types are void, int or Int.
    public abstract java.lang.Object deleteProject(@org.jetbrains.annotations.NotNull()
error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
    kotlin.coroutines.Continuation<? super kotlin.Unit> continuation);

反道:

Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
   > java.lang.reflect.InvocationTargetException (no error message)

但是,我的 ProjectDao.kt 文件有以下内容:

@Dao
interface ProjectDao {
    @Query("SELECT * FROM table_projects")
    fun getAll(): List<Project>

    @Insert
    suspend fun insertProject(project: Project): Long

    @Insert
    fun insertProjects(projects: List<Project>)

    @Delete
    suspend fun deleteProject(project: Project)

    @Query("DELETE FROM table_projects")
    suspend fun deleteAllProjects()

    @Transaction
    @Query("SELECT * FROM table_projects")
    fun getAllProjectsWithCounters(): List<ProjectWithCounters>

    @Transaction
    @Query("SELECT * FROM table_projects WHERE id_project=:projectID")
    fun getProjectWithCounters(projectID: Long): ProjectWithCounters
}

我之前没有遇到过任何问题,突然间我收到了这些错误,我不知道是什么原因造成的。

谢谢!

【问题讨论】:

  • 你对room-ktx有依赖吗?请附上您的build.gradle

标签: android kotlin android-room


【解决方案1】:

设置 kotlin 版本为 1.5.21 或 1.5.31
Kotlin 1.6.0 不能在 ROOM @QUERY 中使用 suspend
选择以下解决方案之一

  1. 打开你的根 build.gradle 并添加这个
    classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21'
  2. 打开您的模块 build.gradle 并将 room-version 2.4.0-alpha03 之一更改为 2.4.0-beta01
    def roomVersion = "2.4.0-alpha03"

【讨论】:

    【解决方案2】:

    使用@Delete注解,你必须定义一个返回数据类型:

    DELETE 查询方法必须返回 void 或 int(删除的行数)。

    所以这应该是:

    @Delete
    suspend fun deleteProject(project: Project): Integer
    

    这将在成功时返回 1,当 project 在数据库中不存在时返回 0

    【讨论】:

    • 如果方法返回不明确,那将是Unit,然后编译为 Void?
    • 这实际上是一个interface 定义-错误消息使您的论点无效。如果是这种情况,则不需要定义返回类型 IntegerVoid - 从中​​生成实际方法。
    【解决方案3】:

    我解决了这个问题如下

    我将 Kotlin 版本设置为 1.6.10 而不是 Room 设置为 2.4.2

    【讨论】:

      猜你喜欢
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      • 2021-11-18
      • 2018-08-13
      • 1970-01-01
      • 2017-03-11
      • 1970-01-01
      相关资源
      最近更新 更多