错误:参数的类型必须是用@Entity注释的类或其集合/数组

Zay*_*han 1 android kotlin android-room

我看过其他有关相同内容的帖子,但我不明白我一定犯了什么错误。我在类的实体和数据库声明文件中声明了相同的名称。我还传递了与实体类名称相同类型的参数,但仍然抛出此错误并且未编译。这是我的代码。TIA

 @Entity(tableName = "current_task_table")
  data class CurrentTask (
    @PrimaryKey(autoGenerate = true) val uid: Int,
    @ColumnInfo(name = "task_name") val taskName: String
)

@Dao
interface CurrentTaskDao {

    @Query("SELECT * FROM current_task_table")
    fun getAllCurrentTask(): LiveData<List<CurrentTask>>

    @Insert
    suspend fun insertCurrentTask(currentTask: CurrentTask)

    @Query("DELETE FROM current_task_table")
    fun deleteAllCurrentTasks()
}

@Database(entities = [CurrentTask::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {

    abstract val currentTaskDao: CurrentTaskDao

    companion object {

        @Volatile
        private var instance: AppDatabase? = null

        fun getInstance(context: Context): AppDatabase? {
            if (instance == null) {
                synchronized(this) {
                    instance = Room.databaseBuilder(
                        context.applicationContext,
                        AppDatabase::class.java, "app_database"
                    )
                        .fallbackToDestructiveMigration()
                        .build()
                }
            }
            return instance
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Zay*_*han 8

suspend通过从 DAO 中删除来修复它

您不能对 DAO 使用挂起方法。在编译时处理的挂起函数,编译器会更改该函数的签名(不同的返回类型,状态机回调的附加参数)以使其非阻塞。Room 等待特定的方法签名来生成代码。因此,在 Room 不直接支持协程之前,您无法使用 DAO 的挂起功能。

在这里找到我的答案: Error with Room dao class when using Kotlin coroutines