简体   繁体   中英

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(); 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

Now, how do i do these 6 different calls asyncronous, and when all 6 are done -> do something (like dimiss a progress bar) ? And when each call finishes, i'll be getting the data returned the call and inserting via 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)

You should use the zip operator like that :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM