0
votes

I'm using MVVM architecture pattern for room database when use Livedata and update row, it immediately shows the changes in the recylerview.

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

But i want use Rxjava instead of livedata in mvvm to display data and changes but when updata row, it doesn't immediately show the changes in the recyclerview.this is my code:

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)

     }

method for get list in fragment

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

        setupRecyclerView(it, context)

    })
}

method for update row

      lessonViewModel.updateLessonRx(bookmarkLesson)

What should I do and which part should I fix?

1

1 Answers

0
votes

Try changing the return type from Single<List<LessonEntity>> to Flowable<List<LessonEntity>> in your DAO