Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rxjava2 flowable not firing onComplete

Here is the code

    private fun setUpDistrictSpinner() {
    commonRepo.getAllDistricts()
            .observeOn(AndroidSchedulers.mainThread())
            .flatMap { list -> Flowable.fromIterable(list) }
            .map { district ->
                districtNameList.add(district.district_name)
                district
            }.subscribe(object:Subscriber<District>{
        override fun onComplete() {
            labSelectionInterface.loadDistricts(districtNameList)
            Timber.d("district List loaded total " + districtList.size)
        }

        override fun onError(t: Throwable?) {
            t!!.printStackTrace()
        }

        override fun onNext(t: District) {
            districtList.add(t)
        }

        override fun onSubscribe(s: Subscription) {
            s.request(Long.MAX_VALUE)
        }
    })

}

onNext is firing but onComplete will not fire. There is also no error message. I am loading list of district into a spinner. Using Room database and Kotlin This is where I get the Flowable

    fun getAllDistricts(): Flowable<List<District>> {
    Timber.d("District list accessed")
    return appDatabase.districtDao().getAllDistricts()
            .subscribeOn(Schedulers.io())

}
like image 243
anil90 Avatar asked Jul 22 '26 04:07

anil90


1 Answers

The Flowable returned by Room never completes. It allows you to receive update upon database modifications.

Here’s how the Flowable behaves:

  1. When there is no user in the database and the query returns no rows, the Flowable will not emit, neither onNext, nor onError.
  2. When there is a user in the database, the Flowable will trigger onNext.
  3. Every time the user data is updated, the Flowable object will emit automatically, allowing you to update the UI based on the latest data.

If you want to receive onComplete event, switch to Maybe. You should read this article.

like image 200
Benjamin Avatar answered Jul 23 '26 17:07

Benjamin