To expand Kenβs answer , another option is to βforkβ the Observed into two branches using the play operator . This ensures that the original observable is called only once. This is useful if there are some expensive treatments or side effects in the chain:
ConnectableObservable<A> connectable = Observable.fromArray() .replay(); connectable .filter(a -> condition(a)) .// <--- do stuff if condition returns true connectable.connect(); // do this after the first branch connectable .filter(a -> complexCondition(a)) // filter all elements(!) .// <--- do stuff if complex condition returns true connectable .// <- iterate all elements
Remember that all branches must handle both onNext and onError events.
source share