RxJava: difference between doOnNext and doOnEach

JavaRx Java

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVasyaFromRussiaView Question on Stackoverflow
Solution 1 - JavaSimon BasléView Answer on Stackoverflow