如何使用RxJava-Android等待多个嵌套的异步调用? [英] How to wait multiple nested async calls by using of RxJava-Android?
问题描述
我是RxJava的新手,这是我的情况,
I'm new to RxJava, here's my case,
- 发送请求A并返回
List<A>
- 对于每个A,发送请求AA,然后返回AA,然后将A和AA绑定
- 有B&逻辑相似的BB
- 仅在所有请求完成后执行操作
- send request A and will get
List<A>
back - for each A, send request AA and will get AA back, bind A and AA then
- there is B & BB with similar logic
- do something only after all requests complete
示例:
request(url1, callback(List<A> listA) {
for (A a : listA) {
request(url2, callback(AA aa) {
a.set(aa);
}
}
}
A和B是独立的
如何构建代码?我还使用Retrofit作为网络客户端.
How to structure the code? I also used Retrofit as network client.
推荐答案
好的,我认为这应该可以解决您问题的第一部分:
OK, I think this should solve the first part of your problem:
请注意,对flatMap
的第二次调用具有2个参数-flatMap
的一个版本不仅为每个输入项生成一个Observable,而且还采用了第二个函数,该函数又将合并来自每个输入项的每个项产生的Observable和相应的输入项.
Notice that the second call to flatMap
is given 2 arguments - there is a version of flatMap
that not only produces an Observable for each input item but that also take a second function which in turn will combine each item from the resulting Observable with the corresponding input item.
请查看此标题下的第三幅图,以直观了解:
Have a look at the third graphic under this heading to get an intuitive understanding:
Observable<A> obeservableOfAs = retrofitClient.getListOfAs()
.flatMap(new Func1<List<A>, Observable<A>>() {
@Override
public Observable<A> call(List<A> listOfAs) {
return Observable.from(listOfAs);
}
)}
.flatMap(new Func1<A, Observable<AA>>() {
@Override
public Observable<AA> call(A someA) {
return retrofitClient.getTheAaForMyA(someA);
}
},
new Func2<A, AA, A>() {
@Override
public A call(A someA, AA theAaforMyA) {
return someA.set(theAaforMyA);
}
})
...
从现在开始,我仍然不确定您要如何继续:您准备好只订阅生成的As的Observable吗?这样,您可以处理每个As(onNext
),也可以等到所有操作都完成(onCompleted
).
From here on I am still not sure how you want to continue: Are you ready to just subscribe to the resulting Observable of As? That way you could handle each of the As (onNext
) or just wait until all are done (onCompleted
).
附录:要将所有项收集到一个列表的末尾,即使用toList()
将Observable<A>
转换为Observable<List<A>>
.
ADDENDUM: To collect all Items into a single List at the end, that is turn your Observable<A>
into an Observable<List<A>>
use toList()
.
https://github.com/ReactiveX/RxJava/wiki/Mathematical-and -Aggregate-Operators#tolist
所以你有
Observable<List<A>> observableOfListOfAs = observableOfAs.toList();
如果您需要对列表的构建进行更细粒度的控制,则也可以使用reduce
.
If you need more fine grained control over the construction of your list, you can also use reduce
.
https://github.com/ReactiveX/RxJava/wiki/Mathematical-and -Aggregate-Operators#reduce
对于B,只需复制您用于As的整个流程即可.
For the Bs, simply duplicate the whole flow you used for the As.
然后您可以使用zip
等待两个流程完成:
You can then use zip
to wait for both flows to complete:
Observable.zip(
observableOfListOfAs,
observableOfListOfBs,
new Func2<List<A>, List<B>, MyPairOfLists>() {
@Override
public MyPairOfLists call(List<A> as, List<B> bs) {
return new MyPairOfLists(as, bs);
}
}
)
.subscribe(new Subscriber<MyPairOfLists>() {
// onError() and onCompleted() are omitted here
@Override
public void onNext(MyPairOfLists pair) {
// now both the as and the bs are ready to use:
List<A> as = pair.getAs();
List<B> bs = pair.getBs();
// do something here!
}
});
我想你可以猜出MyPairOfLists
的定义.
I suppose you can guess the definition of MyPairOfLists
.
这篇关于如何使用RxJava-Android等待多个嵌套的异步调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!