RxJava toList – completes only after onComplete is called

 

 mGetActiveHubUpdatesInteractor.getActiveHubUpdates()
                .filter(Optional::isPresent)
                .firstOrError()
                .map(Optional::get)
                .flatMap(activeHub ->
                        mGetActiveHubEventsInteractor.getActiveHubEvents(activeHub, startTime,
                                endTime))
                .flattenAsObservable(list -> list)
                .map(mActiveHubEventUiMapper::toUiActiveHubEvent)
                .toList()
                .subscribe(this::populateEvents, this::handleError);

This is a quick reminder for those of you who use RxJava and the toList() operator.  toList() completes only after the Observable emits onComplete. So keep in mind if your operators after toList never get called.

Here is an article that explains the operator in more detail. The highlight from the article is:

The toList() operator intercepts onNext() calls. Rather than handing the items further down the chain, it catches them instead and adds them to a List (specifically an ArrayList). Only when the source Observable calls onCompleted() will it emit the entire List up the chain, then it will call onCompleted() up the chain.

You may also like...