RxJava: difference between doOnNext and doOnEach

心已入冬 提交于 2019-12-18 12:04:57

问题


In which cases I should use doOnNext, and in which cases 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.


回答1:


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.

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

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



来源:https://stackoverflow.com/questions/28723341/rxjava-difference-between-doonnext-and-dooneach

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!