0

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

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)

Alex
  • 127
  • 3
  • 12
  • Not android, but maybe [this will help](https://stackoverflow.com/questions/39214073/rxjava-instead-of-asynctask/39215031#39215031) – Tassos Bassoukos Jul 01 '17 at 23:41

1 Answers1

3

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

Karim
  • 322
  • 1
  • 10
  • Thanks a lot, i'll try tomorrow and mark the answer as correct if i have some success – Alex Jul 02 '17 at 02:14
  • 1
    Thanks, it works , Action should be a new Observer , for those who may be reading this and don't know what it should be, and Func3 is actually Function3 (or 4, 5 etc.) on RxAndroid btw, – Alex Jul 02 '17 at 23:02