抽象数据库方法必须return一个@Dao注解class或接口

Abstract database methods must return a @Dao annotated class or interface

我认为这个话题已经存在了。但是,由于我们有一个新版本的房间数据库,这将有助于我更好地理解。 因此,我正在使用 2.3.0 版实现房间数据库,但出现了很多错误:

  1. Type of the parameter must be a class annotated with @Entity or a collection/array of it. kotlin.coroutines.Continuation<? super kotlin.Unit> continuation);
  2. Not sure how to handle insert method's return type. public abstract java.lang.Object insertStudent(@org.jetbrains.annotations.NotNull()
  3. Abstract database methods must return a @Dao annotated class or interface. public abstract void setConnectDatabaseDao(@org.jetbrains.annotations.NotNull()

如前所述here,我添加了 ktx 扩展以支持 Dao 中的可挂起方法。但它仍然给我这些错误。 这是我的代码: 应用构建gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android-extensions'
    id 'kotlin-android'
    //id 'kotlin-parcelize'
}
apply plugin: 'kotlin-kapt'
...
//room data base
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
    kapt "androidx.lifecycle:lifecycle-compiler:2.4.0"
    kapt "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-ktx:$room_version"

    // optional - RxJava2 support for Room
    implementation "androidx.room:room-rxjava2:$room_version"


    // optional - Guava support for Room, including Optional and ListenableFuture
    implementation "androidx.room:room-guava:$room_version"

    // optional - Test helpers
    testImplementation "androidx.room:room-testing:$room_version"

    // optional - Paging 3 Integration
    implementation "androidx.room:room-paging:2.4.0-rc01"

数据库

@Database(entities = [Skill::class, Student::class], version = 1, exportSchema = false)
abstract class ConnectDatabase():RoomDatabase() {

    abstract var connectDatabaseDao:ConnectDatabaseDao
    companion object{
        @Volatile
        var INSTANCE:ConnectDatabase? = null
        fun getInstance(context:Context): ConnectDatabase {
            synchronized(this) {
                var instance = INSTANCE
                if (instance == null) {
                    instance = Room.databaseBuilder(
                        context,
                        ConnectDatabase::class.java,
                        "connect_database"
                    )
                        .fallbackToDestructiveMigration()
                        .build()
                    INSTANCE = instance
                }
                return instance
            }
        }
    }

}


@Dao
interface ConnectDatabaseDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertStudent(student: Student)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertSkill (skill: Skill)

    @Delete
    suspend fun deleteStudent(student: Student)

    @Delete
    suspend fun deleteSkill(skill: Skill)

    @Query("SELECT * FROM student WHERE id = :id")
    suspend fun getStudent(id:Long):Student?

    @Query("SELECT * FROM skill WHERE id = :id")
    suspend fun getSkill(id:Long):Skill?

    @Query("SELECT * FROM student")
    fun getAllStudent():LiveData<List<Student>?>

    @Query("SELECT * FROM skill")
    fun getAllSkill():LiveData<List<Skill>?>

}

数据class

@Parcelize
@Entity(tableName = "student",
    foreignKeys = arrayOf(
        ForeignKey(entity = Skill::class,
            parentColumns = arrayOf("id"),
            childColumns = arrayOf("id_skill"),
            onDelete = ForeignKey.CASCADE
            )
        )
)
data class Student (
        @PrimaryKey(autoGenerate = true)
        @ColumnInfo(name = "id")
        val id: Long,

        @ColumnInfo(name = "name")
        val name: String,

        @ColumnInfo(name= "email")
        val email: String,

        @ColumnInfo(name = "id_skill")
        val idSkill: Long
        ):Parcelable
@Parcelize
@Entity(tableName = "skill")
data class Skill (
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    val id: Long,

    @ColumnInfo(name = "item")
    val item:String
): Parcelable

请问有人知道是什么造成了这个问题吗?

一期如标题所示:-

abstract var connectDatabaseDao:ConnectDatabaseDao

应该是

abstract fun getConnectDatabaseDao(): ConnectDatabaseDao

其他问题可以通过使用 SDK 30 并从 suspend fun .... 中删除 suspend 并在使用 2.3.0 时只有 fun .... 来解决。

例如