java RxJava:d​​oOnNext 和 doOnEach 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28723341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:04:33  来源:igfitidea点击:

RxJava: difference between doOnNext and doOnEach

javarx-java

提问by VasyaFromRussia

In which cases I should use doOnNext, and in which cases doOnEach?

在哪些情况下我应该使用 doOnNext,在哪些情况下我应该使用 doOnEach?

 .doOnEach(new Action1<Notification<? super MessageProfile>>() {
                @Override
                public void call(Notification<? super MessageProfile> notification) {

                }
            })
 .doOnNext(new Action1<MessageProfile>() {
                @Override
                public void call(MessageProfile profile) {
                    messageProfileDao.save(profile);
                }
            })

This looks like the both operators have the same effect.

这看起来两个运算符具有相同的效果。

回答by Simon Baslé

They are indeed quite close. One thing that differs (and it's maybe not that clear in the javadoc actually, more visible in sourcecode) is that in doOnEach, you also get Notificationwrappers for errors and completion event.

他们确实非常接近。不同的一件事(实际上在 javadoc 中可能不是那么清楚,在源代码中更明显)是在 doOnEach 中,您还可以获得Notification错误和完成事件的包装器。

You can then check isOnNext, isOnCompletedor isOnErrorto check the actual kind of event you got a notification for.

然后isOnNext,您可以检查,isOnCompletedisOnError检查您收到通知的实际事件类型。

So one Action.callto rule them all, instead of a fully fledged Observer

所以一个人Action.call来统治他们,而不是一个完全成熟的人Observer