如何通过使用RxJava-的Android等多张嵌套的异步调用? [英] How to wait mulitple nested async calls by using of RxJava-Android?
问题描述
我是新来RxJava,这里是我的情况,
I'm new to RxJava, here's my case,
- 发送请求,并会得到
列表< A>
回 - 每个A,发送请求,AA和将获得AA回来,结合A和AA然后
- 有B和; BB有类似的逻辑
- 只有所有的请求之后做一些完整的
例如:
request(url1, callback(List<A> listA) {
for (A a : listA) {
request(url2, callback(AA aa) {
a.set(aa);
}
}
}
A和B是独立的。
A and B are independent
如何构建code?我也用作为改造网络客户端。
How to structure the code? I also used Retrofit as network client.
感谢。
推荐答案
OK,我想这应该解决您的问题的第一部分:
OK, I think this should solve the first part of your problem:
请注意,第二个呼叫 flatMap
给定2个参数 - 有 flatMap
的一个版本,不仅产生一个可观察到的每个输入项目,但也采取这反过来将每个项目从所得观测结合与相应的输入项目的第二函数。
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:
<一个href=\"https://github.com/ReactiveX/RxJava/wiki/Transforming-Observables#flatmap-concatmap-and-flatmapiterable\">https://github.com/ReactiveX/RxJava/wiki/Transforming-Observables#flatmap-concatmap-and-flatmapiterable
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);
}
})
...
从这里开始,我仍然不知道要如何继续:你准备好只订阅产生的观测作为?这样,你可以处理每个作为( 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
).
附录::要收集所有项目成一单到底名单,那就是把你的观测和LT; A&GT;
到观测与LT;名单&LT; A&GT;方式&gt;
使用了ToList()
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()
.
<一个href=\"https://github.com/ReactiveX/RxJava/wiki/Mathematical-and-Aggregate-Operators#tolist\">https://github.com/ReactiveX/RxJava/wiki/Mathematical-and-Aggregate-Operators#tolist
所以,你有:
Observable<List<A>> observableOfListOfAs = observableOfAs.toList();
如果您需要更细粒度控制列表的建筑,你也可以使用减少
。
If you need more fine grained control over the construction of your list, you can also use reduce
.
<一个href=\"https://github.com/ReactiveX/RxJava/wiki/Mathematical-and-Aggregate-Operators#reduce\">https://github.com/ReactiveX/RxJava/wiki/Mathematical-and-Aggregate-Operators#reduce
有关的旅馆,简单重复你用于作为整个流程。
For the Bs, simply duplicate the whole flow you used for the As.
您就可以使用拉链
等待两个流向完成:
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
。
这篇关于如何通过使用RxJava-的Android等多张嵌套的异步调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!