简体   繁体   中英

OnCompleted called inside zip

Observable<List<Object1>> obs1 = getObservableList1().from({/*some code*/
}).map({/*some code*/}).toList({/*some code*/});

Observable<List<Object2>> obs2 = getObservableList2();

Observable.zip(obs1 , obs1, (res1, res2) -> {

//some more code block1

}).subscribe({

//some code after obs1 and obs2 
//some more code block2

});

I have situation when obs1 contains empty list and zip.subscribe() is never called. But I need just return empty List as res1 and execute block1 and block2 like if the list from obs1 have elements.

The question is how to skip from().map().toList() from obs1 and return empty List as res1 .

It depends, what do you want to achieve? If you want to just execute some code after both the source Observables complete, use the following:

 Observable.merge(obs1 , obs2).doOnComplete(() -> {
    ....
 }).subscribe(...)

If instead you need to do something with the result list, but obs2 is sometimes empty (no items), then you just need to set a default:

 Observable<List<Object2>> obs2 = getObservableList2()
         .defaultIfEmpty(Collections.emptyList());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM