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

Vas*_*sia 40 java rx-java

在哪些情况下我应该使用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);
                }
            })
Run Code Online (Sandbox Code Playgroud)

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

Sim*_*slé 48

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

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

所以Action.call要统治所有人,而不是完全成熟Observer