Exploring RxJava in Android — Operators for Combining Observables
This article is part of RxJava Introduction series. You can checkout the entire series here:
- RxJava Introduction and Operators for creating Observables
- RxJava Operators: Operators for Transforming Observables
- RxJava Operators: Operators for Filtering Observables
- RxJava Operators: Operators for Combining Observables — You are here
- RxJava Operators: Utility Operators
- RxJava Operators: Conditional and Boolean Operators
- RxJava Operators: Mathematical and Aggregate Operators
- RxJava: Different types of Observables
- RxJava: Different types of Subjects
Operators for Combining Observables
CombineLatest
This operator is used when an item is emitted by either of two Observables, and the latest item emitted by each Observable is combined via a specified function and the resulting items are emitted based on the results of this function.
Sample Implementation: The below code demonstrates the use of combineLatest()
operator. Let’s say there are 2 Observables each emitting values after an interval of 100 ms and 150 ms respectively. The combineLatest()
operator combines both the observables and emits the result at each particular intervals.
Output
Refreshed Observable1: 0 refreshed Observable2: 1
Refreshed Observable1: 0 refreshed Observable2: 2
Refreshed Observable1: 0 refreshed Observable2: 3
Refreshed Observable1: 1 refreshed Observable2: 3
Refreshed Observable1: 1 refreshed Observable2: 4
Join
Whenever two items (each one for one source) are overlapped, they will be paired and sent to the resultSelector which computes and returns them. The join()
operator takes the following items:
- right — the second Observable to join items from.
- leftDurationSelector — a function to select a duration for each item emitted by the source Observable, used to determine overlap.
- rightDurationSelector — a function to select a duration for each item emitted by the right Observable, used to determine overlap.
- resultSelector — a function that computes an item to be emitted by the resulting Observable for any two overlapping items emitted by the two Observables.
Sample Implementation: The below code demonstrates the use of join()
operator. In the below sample, we create two Observables: left & right which emits a value every 100 milliseconds. We use the join()
operator to join the left Observable to the right Observable. The two integers emitted from both the Observables are added and the result is printed.
Output
Left result: 0 Right Result: 0
onNext: 0
Left result: 1 Right Result: 1
onNext: 2
Left result: 2 Right Result: 2
onNext: 4
Left result: 3 Right Result: 3
onNext: 6
Left result: 5 Right Result: 5
onNext: 10
Merge
This operator combines multiple Observables into one by merging their emissions i.e. merges multiple Observables into a single Observable but it won’t maintain the sequential execution. merge()
operator doesn’t wait for data from observable 1 to complete. It emits data from both the observable simultaneously as soon as the data becomes available to emit.
Sample Implementation: The below code demonstrates the use of merge()
operator.
Output
onNext: B0
onNext: A0
onNext: A1
onNext: B1
onNext: A2
onNext: B2
onNext: B3
onNext: A3
onNext: A4
onNext: B4
Concat
This operator combines the output of two or more Observables into a single Observable, without interleaving them i.e. the first Observables completes its emission before the second starts and so forth if there are more observables.
Sample Implementation: The below code demonstrates the use of concat()
operator.
Output
onNext: A0
onNext: A1
onNext: A2
onNext: A3
onNext: A4
The difference between
merge()
andconcat()
is thatmerge()
interweaves output whileconcat()
waits for earlier emissions to complete before processing new emissions.
Zip
This operator combines the emissions of multiple Observables together via a specified function and emit single items for each combination based on the results of this function.
Sample Implementation: The below code demonstrates the use of zip()
operator.
Output
onNext: A0 B0
onNext: A1 B1
onNext: A2 B2
onNext: A3 B3
onNext: A4 B4
SwitchOnNext
This operator emits items from the first Observable until the second Observable start emitting. Then, it unsubscribes from the first Observable and start emitting items from the second one.
Sample Implementation: The below code demonstrates the use of switchOnNext()
operator.
Output
onNext: 0
onNext: 1
onNext: 2
onNext: 0
onNext: 1
onNext: 2
onNext: 0
That’s it guys! This is part four of the series on RxJava. I hope you enjoyed this article and found it useful, if so please hit the Clap button. Let me know your thoughts in the comments section.
Happy coding!