【问题标题】:Room query with id doesn't return the right list RxJava MVVM architecture带有 id 的房间查询不返回正确的列表 RxJava MVVM 架构
【发布时间】:2020-08-17 09:46:04
【问题描述】:

我的查询返回有问题,我有一个包含另一个表中的字符串 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,我得到了结果。 请问有什么帮助吗? 谢谢

【问题讨论】:

    标签: kotlin mvvm rx-java android-room


    【解决方案1】:

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

    默认情况下,Room 使用类名作为数据库表名,我认为它是区分大小写的,所以你的查询应该是"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>>
    
    }
    

    你说:...我从我的 api 调用中填充的房间数据库

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-28
      • 2022-06-16
      • 2022-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      相关资源
      最近更新 更多