gpt4 book ai didi

android - Rxjava2 可流动不触发 onComplete

转载 作者:太空宇宙 更新时间:2023-11-03 11:36:33 24 4
gpt4 key购买 nike

这是代码

    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 正在触发,但 onComplete 不会触发。也没有错误消息。我正在将地区列表加载到微调器中。使用 Room 数据库和 Kotlin这是我得到 Flowable 的地方

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

}

最佳答案

Room 返回的 Flowable 永远不会完成。它允许您在数据库修改时接收更新。

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.

如果你想接收onComplete事件,切换到Maybe。你应该阅读这个 article .

关于android - Rxjava2 可流动不触发 onComplete,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46541385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com