Subscribe to 2 different Observable and onNext both of them?

Given that I subscribe to 2 different Observables, and I want to get both of them onnext after doing some operations with them

let's say i have 2 observables

Observable<List<String>> childName = Observable.from(children)... some operations
Observable<List<String>> teacherName = Observable.from(teachers)... some operations

How do I get both of them on my subscription?

subscribe( 
    onNext(List<String> childName, List<String> className)

so that I can go through both of them in my listener this way.

I don’t want to combine them, I just want to receive both of them after the operation is completed and transfer them to my subscriptions

+4
source share
2 answers

You can ziptheir values ​​in Pair:

Observable.zip(childName, className, 
    (a, b) -> Pair.of(a, b))
.subscribe((Pair<List<String>, List<String>> pair) -> {
    // do something with pair.first and pair.second
}, Throwable::printStackTrace);
+3
source

hacked but simple

Observable.zip(childName, teacherName, (childList, teachersList) -> {
    // handle childList & teachersList
    return Observable.empty();
}).subscribe(o -> {}, error -> {
    //handle errors
});
0
source

Source: https://habr.com/ru/post/1660327/


All Articles