Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava: difference between doOnNext and doOnEach

Tags:

java

rx-java

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.

like image 514
VasyaFromRussia Avatar asked Feb 25 '15 15:02

VasyaFromRussia


People also ask

What is doOnNext?

doOnNext operator called every time when source Observable emits an item. It can be used for debugging purposes, applying some action to the emitted item, logging, etc... Observable. range(1, 3) .

What is doOnNext in reactor?

1. Since doOnNext provides you the element flowing through the pipeline with a Consumer , it is after. Order is something like this: source emits item -> doOnNext lambda is triggered -> subscriber onNext is called: github.com/reactor/reactor-core/blob/master/reactor-core/src/…


1 Answers

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

like image 81
Simon Baslé Avatar answered Oct 04 '22 12:10

Simon Baslé