RxAndroid,如何检测observable是否已完成排放

时间:2017-07-19 16:09:42

标签: android kotlin rx-java rx-android rx-kotlin

我正在编写以下代码片段以从firebase数据库中获取已保存食物的列表,然后使用该列表,我再次从firebase数据库中获取个别食物详细信息。

以下代码正常工作,除了我无法弄清楚如何让第二个flatMap知道第一个flatMap的发射已经完成(所有食物清单已经处理)。所以我无法调用onCompleted()方法,因此无法检测整个过程何时结束。

查看以下代码段中的评论:

Observable.create<List<PersonalizedFood>> {

            FirebaseDTDatabase.getSavedDietFoodQuery(user.uid).addListenerForSingleValueEvent(object : ValueEventListener {
                override fun onCancelled(p0: DatabaseError?) {

                }

                override fun onDataChange(p0: DataSnapshot?) {
                    val list = ArrayList<PersonalizedFood>()
                    p0?.let {
                        for (dateObject in p0.children) {
                            for (foodItem in dateObject.children) {
                                val food = foodItem.getValue(FBPersonalizedFood::class.java) as FBPersonalizedFood
                                list.add(PersonalizedFood(food))
                            }
                        }
                    }
                    it.onNext(list)
                    it.onCompleted()
                }
            })
        }.subscribeOn(Schedulers.io()).flatMap {
            Observable.from(it) // returning a Observable that emits items of list ("it" is the list here) 
        }.observeOn(Schedulers.io()).flatMap {
        // How does this flatMap know that emission of all item has been finished so that onCompleted() method could be called.
            personalizedFood ->

            Observable.create<Boolean>{
                FirebaseDTDatabase.getFoodListReference(personalizedFood.foodId).addListenerForSingleValueEvent(object :ValueEventListener{
                    override fun onCancelled(p0: DatabaseError?) {
                        it.onError(p0?.toException())
                    }

                    override fun onDataChange(p0: DataSnapshot?) {
                        if(p0 != null) {
                            val food = p0.getValue(FBFood::class.java)!!
                            val repo = LocalFoodRepository()
                            doAsync {
                                repo.insertFood(this@LoginActivity, Food(food.foodId, food.foodName, food.foodDesc))
                                repo.insertServingDetails(this@LoginActivity, food.servingList.map { it.component2() })
                                repo.saveFood(this@LoginActivity, personalizedFood)
                                it.onNext(true)
                            }

                        }else {
                            it.onNext(false)
                        }
                    }

                })
            }
        }.observeOn(Schedulers.io()).doOnCompleted{
            dismissProgressDialog()
            finish()
        }.doOnError{
            it.printStackTrace()
            dismissProgressDialog()
            finish()
        }.subscribe()

感谢。

1 个答案:

答案 0 :(得分:2)

来自Observable的{​​{1}}知道&#34;何时所有项目都已完成&#34;当它发出的所有可观察量都调用flatMap时。代码中的第二个onCompleted()从不调用flatMap,因为它创建的所有可观察对象都不会调用onCompleted()

您应该在onCompleted()方法中致电onCompleted()。由于在onDataChange()中创建的每个可观察项只发出一个项,因此可以在flatMap方法之后直接调用它:

onNext()