RxJava's combineLatest()
acts like a particular type of zip()
. As we already learned, zip()
works on the latest unzipped item of the two Observables. Instead, combineLatest()
works on the latest emitted items: if Observable1
emits A and Observable2
emits B and C, combineLatest()
will process AB and AC pairs, as shown in the following figure:
The combineLatest()
function takes up to nine Observables as parameters, or even a list of Observables, if needed.
Borrowing the loadList()
function from the previous example, we can modify it to achieve a real-world example for combineLatest()
:
private void loadList(List<AppInfo> apps) { mRecyclerView.setVisibility(View.VISIBLE); Observable<AppInfo> appsSequence = Observable.interval(1000, TimeUnit.MILLISECONDS) .map(position ->apps.get(position.intValue())); Observable<Long> tictoc = Observable.interval(1500, TimeUnit.MILLISECONDS); Observable .combineLatest(appsSequence, tictoc...