本文介绍了RxJava:d​​oOnNext和doOnEach之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

 .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.

推荐答案

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

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 Notification wrappers for errors and completion event.

然后可以检查 isOnNext isOnCompleted isOnError ,以检查收到通知的实际事件类型.

You can then check isOnNext, isOnCompleted or isOnError to check the actual kind of event you got a notification for.

因此,只需一个 Action.call 即可统治所有规则,而不是完全成熟的 Observer

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

这篇关于RxJava:d​​oOnNext和doOnEach之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 18:01