Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skipping last duplicate with RxJava

Suppose we have a subject that we use to publish some events

subject.onNext(...)

the end Subscriber is subscribed trough an Observable which was created from this subject...

I wonder if we can add some logic to observable so that if it gets the same value it wont propogate it to subscribers... or someting more complex..?

Is there a possibility to create an observable from subject that executes some code before emiting value to its subscribers?

like image 209
vach Avatar asked Mar 09 '15 12:03

vach


3 Answers

There is a distinctUntilChanged operator that skips a value if it was emitted more than once in a row.

If you need more general behaviour and want to skip values based on some condition, then filter is probably what you are looking for.

like image 174
Vladimir Mironov Avatar answered Sep 18 '22 07:09

Vladimir Mironov


Seems distictUntilChanged is exactly what is necessary in this case...

like image 23
vach Avatar answered Sep 22 '22 07:09

vach


filter seems what i want but i dont really know how do i keep last value somewhere (unless i implement custom observable) and how do i access it to compare with new value?

You could use .buffer(2, 1).filter(...), so that filter has access to the current and to the last element.

like image 36
Samuel Gruetter Avatar answered Sep 21 '22 07:09

Samuel Gruetter