[英]subscribe 2 different Observable and onNext both of them?
Given I subscribe to 2 different Observables and I want get both of them on onNext after do some operations to them 鉴于我订阅了2个不同的Observable,在对它们进行一些操作之后,我想将它们都放在onNext上
let's say I have 2 Observables 假设我有2个可观测值
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 subscribe? 如何在订阅中同时获取它们?
subscribe(
onNext(List<String> childName, List<String> className)
so that I can pass both of them in my listener in that manner. 这样我就可以以这种方式将它们都传递给我的听众。
I don't want to combine them, I just want when after the operations are finished, get both of them and pass it on my subscriptions 我不想合并它们,我只想在操作完成后什么时候将它们全部获取并传递给我的订阅
You can zip
their values into a Pair
: 您可以将它们的值
zip
为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);
hacky but simple hack但是简单
Observable.zip(childName, teacherName, (childList, teachersList) -> {
// handle childList & teachersList
return Observable.empty();
}).subscribe(o -> {}, error -> {
//handle errors
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.