Используйте Rx java для обновления и отображения данных в базе данных комнаты - PullRequest
0 голосов
/ 08 июля 2020

Я использую шаблон архитектуры MVVM для базы данных комнаты при использовании Livedata и строки обновления, он сразу показывает изменения в recylerview.

   @Query("select * from lessons_table where course_id = :courseId")
   fun getListLessonDb(courseId:Int):LiveData<List<LessonEntity>>

Но я хочу использовать Rx java вместо liveata в mvvm для отображения данных и изменений, но при обновлении строки он не сразу показывает изменения в recyclerview. это мой код:

Dao

 @Dao
interface LessonDao {

@Query("select * from lessons_table where course_id = :courseId")
fun getListLessonDbRx(courseId: Int): Single<List<LessonEntity>>

@Update
fun updateLessonRX(lessonEntity: LessonEntity): Completable

 }

LessonRepository

class LessonRepository(val application: Application) {

val lessonDao: LessonDao

init {
    val db = ArabiAraghiTutorialDatabase.getDataBase(application)
    lessonDao = db!!.lessonDao()
}
 
     fun getListFromDb(courseId: Int): LiveData<List<LessonEntity>> {
    val result: MutableLiveData<List<LessonEntity>> = MutableLiveData()
    getObservable(courseId).observeOn(Schedulers.io())
        .subscribeOn(AndroidSchedulers.mainThread())
        .subscribe(object : SingleObserver<List<LessonEntity>> {

            override fun onSuccess(t: List<LessonEntity>) {
                result.postValue(t)


            }


            override fun onSubscribe(d: Disposable) {

            }


            override fun onError(e: Throwable) {


            }


        })
    return result
}

 fun getObservable(courseId: Int): Single<List<LessonEntity>> = lessonDao.getListLessonDbRx(courseId)

 fun updateLessenDbRx(lessonEntity: LessonEntity) {


    lessonDao.updateLessonRX(lessonEntity).observeOn(AndroidSchedulers.mainThread())
        .observeOn(Schedulers.io()).subscribe(object : CompletableObserver {
            override fun onComplete() {
          

            }

            override fun onSubscribe(d: Disposable) {

            }

            override fun onError(e: Throwable) {

            }
        })
}
  

 

  
}

ViewModel

    class LessonViewModel(application: Application):AndroidViewModel(application) {

private val lessonRepository=LessonRepository(application)

   fun getListLessonFromDb(courseId: Int):LiveData<List<LessonEntity>> = lessonRepository.getListFromDb(courseId)


fun updateLessonRx(lessonEntity: LessonEntity) = lessonRepository.updateLessenDbRx(lessonEntity)

     }

метод получения списка во фрагменте

   private fun getListLessonDb(courseId: Int, context: Context) {
    lessonViewModel.getListLessonFromDb(courseId).observe(viewLifecycleOwner, Observer {

        setupRecyclerView(it, context)

    })
}

метод обновления строки

      lessonViewModel.updateLessonRx(bookmarkLesson)

Что делать и какая часть должна Исправлю?

1 Ответ

0 голосов
/ 08 июля 2020

Попробуйте изменить тип возвращаемого значения с Single<List<LessonEntity>> на Flowable<List<LessonEntity>> в вашем DAO

...