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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s