带有 id 的房间查询不 return 正确列表 RxJava MVVM 架构

Room query with id doesn't return the right list RxJava MVVM architecture

我的查询有问题 returns,我有一个学生 class 包含来自另一个 table

的字符串 ID
data class StudentEntity(
        @PrimaryKey
        val idStudent: String,
        val classId: String,
        val name: String,
        val notes: Note?,
)

我还创建了一个房间数据库,我正在从我的 api 电话

中填充该数据库
@Database(entities = [Student::class, Note::class], version = 14, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
    abstract fun programDAO(): ProgramDAO

    companion object{
        @Volatile
        private var INSTANCE: AppDatabase? = null
        fun getInstance(context: Context) : AppDatabase {
            synchronized(this) {
                var instance = INSTANCE
                if (instance == null) {
                    instance = Room.databaseBuilder(
                        context.applicationContext, AppDatabase::class.java, "student-database"
                    ).fallbackToDestructiveMigration().build()
                    INSTANCE = instance
                }
                return instance
            }
        }
    }

}

为此,我有一个 programDao 可以帮助我 运行 我的查询

@Dao
interface ProgramDAO {

    @Transaction
    @Query("select * from studentEntity")
    fun getStudents(): Single<List<StudentEntity>>

    @Transaction
    @Query("select * from studentEntity where classId = :classid")
    fun getStudentsWithId(classid: String): Single<List<StudentEntity>>

}

为了执行这些查询,我有我的存储库:

class ProgramRepository(val api: ApiService, val programDAO: ProgramDAO) {

    fun getStudentsFromDbWithId(idClass: String) : Observable<StudentEntity>{
        return programDAO.getStudentsWithId(idClass).toObservable()
    }

    fun getStudentsFromDb() : Observable<StudentEntity>{
        return programDAO.getStudents().toObservable()
    }
}

MV让我连接数据和视图:

class ProgramListViewModel(private val programRepository: ProgramRepository) {
  fun getListFromDBWithId(classID: String): Observable<List<StudentEntity>> {
        return programRepository.getStudentsFromDbWithId(deliverySiteId)
    }


   fun getListFromDB(): Observable<List<StudentEntity>> {
        return programRepository.getStudentsFromDb()
    }

}

因此,为了使用数据并在我的片段上获取适配器和 KPI,我没有从数据库中收到正确的列表,我记录了结果以查看我得到了什么,首先,我确实记录没有 ID 的整个列表,我有列表,但是当我使用像这样的 ID 时:

subscribe(programListViewModel.getListFromDBWithId("111").subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                Log.e("list of students", it.toString())
            })

我得到一个空列表,所以我认为问题出在 id 上,我尝试使用从整个列表中记录的 id 但没有用,我还使用 S 启动我的 sql 使用相同的 id 查询,我得到了结果。 有什么帮助吗? 谢谢

你的 AircraftEntity 应该是 StudentEntity 吗?如果是(根据您的其余代码示例,我认为是),请更新您的问题。

默认情况下,Room 使用 class 名称作为数据库 table 名称,我认为它是 case-sensitive,所以您的查询应该是 "select * from StudentEntity"。更好的方法是明确地给它起一个名字:

@Entity(tableName = "students")
data class StudentEntity (
    // ...
)

然后,您的 ProgramDAO 将如下所示:

@Dao
interface ProgramDAO {

    @Query("select * from students")
    fun getStudents(): Single<List<StudentEntity>>

    @Query("select * from students where classId = :classid")
    fun getStudentsWithId(classid: String): Single<List<StudentEntity>>

}

You said: ... a room database that I'm populating from my api call

您在哪里以及如何填充您的数据库?我在您的 DAO 中没有看到任何 @Insert 函数,除非您只是将它们从您的代码片段中删除。如果您的房间数据库未填充,您当然不会获得任何数据。