[英]RxJava / RxAndroid + Retrofit , making 6 different Observable calls asyncronous
i am in the process of learning RxJava / Android (i'm currently combining it with Retrofit for network calls) , now i have a question, say i have 6 different Observables , like this : Observable<Client> clients = apiInterface.getClients() Observable<Orders> orders = apiInterface.getOrders(); Observable<Products> products = apiInterface.getProducts();
我正在学习RxJava / Android(目前正在将它与Retrofit结合使用以进行网络调用),现在我有一个问题,说我有6个不同的Observables,例如:
Observable<Client> clients = apiInterface.getClients() Observable<Orders> orders = apiInterface.getOrders(); Observable<Products> products = apiInterface.getProducts();
Observable<Client> clients = apiInterface.getClients() Observable<Orders> orders = apiInterface.getOrders(); Observable<Products> products = apiInterface.getProducts();
etc. apiInterface being the Retrofit client , and getClients etc. being the calls apiInterface是翻新客户端,而getClients等是调用
Now, how do i do these 6 different calls asyncronous, and when all 6 are done -> do something (like dimiss a progress bar) ? 现在,我该如何异步执行这6个不同的调用,并且在完成所有6个调用后->做点什么(例如取消进度条)? And when each call finishes, i'll be getting the data returned the call and inserting via greenDAO.
当每个调用结束时,我将获得返回调用的数据并通过greenDAO进行插入。 Managed to chain them syncronously until now, but i need them to be fired in parallel tasks (like the 6 AsyncTasks + countDownLatch implementation i have right now for these calls)
设法将它们同步链接到现在,但是我需要在并行任务中将它们触发(例如我现在对这些调用有6个AsyncTasks + countDownLatch实现)
You should use the zip operator like that : 您应该像这样使用zip运算符:
Observable<Client> clients = apiInterface.getClients()
Observable<Orders> orders = apiInterface.getOrders();
Observable<Products> products = apiInterface.getProducts();
Observable<String> clientorders = Observable.zip(
clients,
orders,
products,
new Func3<Client, Orders, Products> {
@Override
public String call(Client client, Orders orders, products products) {
return "progress bar dismissed" ;
}
}
);
clientorders.subscribe(new Action1<String>() {
@Override
public void call(String s) {
//action
//dimiss progress bar
}
})
}
Zip operator : http://reactivex.io/documentation/operators/zip.html 邮编运营商: http : //reactivex.io/documentation/operators/zip.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.