title: Retrofit and RxJava: How to combine two requests and get access to both results?
body: <p>I need to make two requests for services and combine it results:</p>
  
  <p>ServiceA() =>  <code>[{"id":1,"name":"title"},{"id":1,"name":"title"}]</code> </p>
  
  <p>ServiceB(<code>id</code>) =>  <code>{"field":"value","field1":"value"}</code></p>
  
  <p>Currently, I have managed to combine the results, but I need to pass <code>id</code> as a parameter to the <strong>ServiceB</strong> and get access to the first result. </p>
  
  <p>What I tried so far:</p>
  
  <pre><code>   Retrofit repo = new Retrofit.Builder()
                  .baseUrl("https://api.themoviedb.org/3/genre/")
                  .addConverterFactory(GsonConverterFactory.create())
                  .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                  .build();
  
          Observable&lt;GenerosResponse&gt; Genres  = repo
                  .create(services.class)
                  .getAllGeneros("movie","list","da0d692f7f62a1dc687580f79dc1e6a0")
                  .subscribeOn(Schedulers.newThread())
                  .observeOn(AndroidSchedulers.mainThread());
  
          Observable&lt;ResponseMovies&gt; Movies = repo
                  .create(services.class)
                  .getAllMovies("28","movies","da0d692f7f62a1dc687580f79dc1e6a0",12)
                  .subscribeOn(Schedulers.newThread())
                  .observeOn(AndroidSchedulers.mainThread());
  
          Observable&lt;CollectionsMovies&gt; combined = Observable.zip(Genres, Movies, new Func2&lt;GenerosResponse, ResponseMovies, CollectionsMovies&gt;() {
              @Override
              public CollectionsMovies call(GenerosResponse generosResponse, ResponseMovies responseMovies) {
                  return new CollectionsMovies(generosResponse, responseMovies);
              }
          });
  
          combined.
                  subscribeOn(Schedulers.newThread())
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe(...);
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p><em>Solution</em> according to @Maxim Ostrovidov's answer:</p>
  
  <pre><code> private Observable&lt;GenerosResponse&gt; makeRequestToServiceA() {
          return  service.getAllGeneros("movie","list","da0d692f7f62a1dc687580f79dc1e6a0"); //some network call
      }
  
      private Observable&lt;ResponseMovies&gt; makeRequestToServiceB(Genre genre) {
          return service.getAllMovies(genre.getId(),"movies","da0d692f7f62a1dc687580f79dc1e6a0","created_at.asc"); //some network call based on response from ServiceA
      }
  
      void doTheJob() {
  
          makeRequestToServiceA()
          .flatMap(userResponse -&gt; Observable.just(userResponse.getGenres()))      //get list from response
                  .flatMapIterable(baseDatas -&gt; baseDatas)
                  .flatMap(new Func1&lt;Genre, Observable&lt;? extends ResponseMovies&gt;&gt;() {
  
                      @Override
                      public Observable&lt;? extends ResponseMovies&gt; call(Genre genre) {
                          return makeRequestToServiceB(genre);
                      }
                  }, new Func2&lt;Genre, ResponseMovies, CollectionsMovies&gt;() {
  
                      @Override
                      public CollectionsMovies call(Genre genre, ResponseMovies responseMovies) {
                          return new CollectionsMovies(genre,responseMovies);
                      }
                  }).
                  subscribeOn(Schedulers.newThread())
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe(....);
      }
  </code></pre>