java - Multiple API calls with retrofit and RXJava 2 -


i have question in order understand better rx java solution retrofit in android, problem following: have application has offline mode local database, while i'm working app, process in there recorded database , send backend saved. in case have process change status in order, example have action change arriving status of job, change job started status, , 4 more actions change statuses in process. statuses saved different local database tables , have make multiple api call change statuses 1 each status. explaning this, have screen in app group process statuses every job pending send backend, in case grouped id of job, press button in app screen , send of statuses calling every api , result when finish calls in order put loading progress , remove when finish, clear local database statuses send well, , finish activity. know rx java has zip option call api's , wait result, in case don't know if it's possible because have call every api every same status of jobs, example: have job 1, 2, 3 , 4.

  1. job 1 - has status 1, status 2, status 3.
  2. job 2 - has status 1, status 2.
  3. job 3 - has status 1, status 2, status 3, status 4.
  4. job 4 - has status 1.

every status has 1 api cosumed. need make process call every api gruped status, call api's , response of every call in order delete statuses internal table , when finish put progressbar = hide , refresh recycler view. woul run in parallel , result when calls finish. don't know if i'm clear problem if not, coud explain more detail or answer questions. please happy help.

based on answer posted here: how can make rxjava zip run in parallel? suggest zipping observables in parallel.

suppose build calls api that:

public class api {     @post("api 1 url")     observable<responseobject> poststatus1(...);      @post("api 2 url")     observable<responseobject> poststatus2(...); } 

you have prepare observables executed in parallel selected job:

private api api;  public list<observable<responseobject>> prepareobservables(job job) {     list<observable<responseobject>> list = new arraylist<>();      //get states given job     for(string status : job.getstates()) {         // check api method need call         if(status.equals("status1") {             //subscribtion on new thread ensure calls executed in parallel                             list.add(api.poststatus1(...).subscribeon(schedulers.newthread()))         }          ...     }      return list; } 

now zip observables , execute

public void runobservables(list<job> pendingjobs) {     list<observable<responseobject>> allobservables = new arraylist<>();      for(job job : pendingjobs) {         list.addall(prepareobservables(job));     }      observable.zip(allobservables, new function<object[], object>() {         @override         public object apply(object[] o) throws exception {             // can combine respones api here         }).subscribe(new consumer<object>() {             // block of code executes when calls api successfull             // need to, i.e. delete jobs db         }); } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -