RxJava: how to code something like doOnEmpty?

此生再无相见时 提交于 2019-12-08 07:05:36

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

source.compose(Transformers.doOnEmpty(action))

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

You can use switchIfEmpty and do something with this fallback Observable

 Observable.just(2, 4, 6)
            .filter(value -> ((value % 2) != 0))
            // replace the empty observable with an empty observable
            // but this observable will log when it will completed 
            .switchIfEmpty(Observable.<Integer>empty().doOnTerminate(() -> System.out.println("empty !")))
            .subscribe();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!