本文介绍了RxJava:如何编写类似doOnEmpty的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Observable上使用了多个过滤器,当结果为空时,我想在过滤结束时报告案例.我无法在处理结束时执行此操作,因为该可观察到的对象应该与另一个对象并置:

I am using several filters on my Observable and I would like to report cases at the end of filtering when the result was empty. I cannot do it at the end of processing because this observable is supposed to be concatenated with another one:

        Observable.just(1, 2, 3)
                .concatWith(
                        Observable.just(2, 4, 6)
                                .filter(value -> ((value % 2) != 0))
                                // report if empty
                )

推荐答案

您可以从Transformers.doOnEmpty在Maven Central上的> rxjava-extras :

You can use Transformers.doOnEmpty from rxjava-extras which is on Maven Central:

source.compose(Transformers.doOnEmpty(action))

如果您关心效率(分配/性能),则可以使用此解决方案,否则请使用@dwursteisen的解决方案.

You might use this solution if you cared about efficiency (allocations/performance) but otherwise use @dwursteisen's solution.

这篇关于RxJava:如何编写类似doOnEmpty的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 00:38