anil90 anil90 - 8 months ago 167
Android Question

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())

}

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download