title: RxJava and Retrofit Chain multiple Calls from diffrent Services and Conditions
body: <p>i don´t get the right way. The Problem is, that i have two Retrofit API´s and Services and need two wait for response and make a decision and based on this other calls.</p>
  
  <p>Example:</p>
  
  <p>APIx:</p>
  
  <pre><code>@GET("xyz/{id}/exists")
  Observable&lt;Exists&gt; checkObjectExists(@Path("id") String id);
  
  @POST("xyz/")
  Observable&lt;Object&gt; addObjectA(@Body Object a);
  </code></pre>
  
  <p>APIy:</p>
  
  <pre><code>@POST("abc/{id}/blabla")
  Observable&lt;Object&gt; addObjectB(@Path("id") String id, @Body Object b);
  </code></pre>
  
  <p>Now the Use case:</p>
  
  <p>I need to do a Request if some Object exists like:</p>
  
  <pre><code>serviceA.exists(id).flatMap(exists -&gt; if(exists) ...
  </code></pre>
  
  <p>if Exists is true then i need to call</p>
  
  <pre><code>serviceB.addObjectB(b)
  </code></pre>
  
  <p>Then the first flow is finish.</p>
  
  <p>if Exists is false i need to call</p>
  
  <pre><code>serviceA.addObject(a) 
  </code></pre>
  
  <p>and then when the i get a Success in onNext i need to call </p>
  
  <pre><code>ServiceB.addObject(b) 
  </code></pre>
  
  <p>again. But i really dont get the Chain with RxJava and Retrofit. I can handle this stuff with a lot lines of Code with something like this:</p>
  
  <pre><code>    private void _checkExists() {
      ServiceA serviceA= ServiceA.create();
      serviceA.checkObjectExists(id)
              .observeOn(AndroidSchedulers.mainThread())
              .subscribeOn(Schedulers.io())
              .subscribe(new Subscriber&lt;Exists&gt;() {
                  @Override
                  public void onCompleted() {
  
                  }
  
                  @Override
                  public void onError(Throwable e) {
                      Timber.d("Something get Wrong");
                  }
  
                  @Override
                  public void onNext(Exists exists) {
                      if(exists) {
                          _addObjectB(b);
                      } else {
                          addobjectA(a);
                      }
                  }
              });
  }
  
  private void addObjectA(Object a) {
      ServiceA serviceA= ServiceA.create();
      serviceA.addObjectA(a)
              .observeOn(AndroidSchedulers.mainThread())
              .subscribeOn(Schedulers.io())
              .subscribe(new Subscriber&lt;Object&gt;() {
                  @Override
                  public void onCompleted() {
  
                  }
  
                  @Override
                  public void onError(Throwable e) {
                      Timber.d("Something get Wrong");
                  }
  
                  @Override
                  public void onNext(Object a) {
                      addObjectB();
                  }
              });
  }
  
  private void addObjectB() {
      ServiceB serviceB= ServiceB .create();
      serviceB.addObjectB(id, b)
              .subscribeOn(Schedulers.io())
              .observeOn(AndroidSchedulers.mainThread())
              .subscribe(new Subscriber&lt;Object&gt;() {
                  @Override
                  public void onCompleted() {
  
                  }
  
                  @Override
                  public void onError(Throwable e) {
                      Timber.d("Something get Wrong");
                  }
  
                  @Override
                  public void onNext(Object b) {
                      Timber.d("Everything is ok");
                  }
              });
  }
  </code></pre>
  
  <p>I tried to chain everything together with flatMap and so on, but it works not correctly in the Chain and also the ErrorHandling was strange, because when i get some Error from the Backend my app will Crash, this is why i add every call the Action3 with OnComplete,OnError,OnNext. Is there a possibility to do this with less code?</p>
  
  <p>Thanks</p>