title: Android RXJava2 Retrofit Handling paging
body: <p>I'm having issues handling paging with RXJava2. I tried to follow this <a href="https://stackoverflow.com/questions/28047272/handle-paging-with-rxjava">post from SO</a> but it was dated. I was hoping to get a more up to date answer. I'm using Retrofit and I'm trying to page a third party API till I have all of the results. The response is below but I pretty much want to through all pages and combine all results.</p>
  
  <p>Currently my code is as follows:</p>
  
  <pre><code>public Observable&lt;WatchList&gt; syncWatchlistSection(String accountId) {
      String sessionToken = mLocal.getSessionId();
      int page = 1;
  
      return getWatchlistFromServerHelper(sessionToken, accountId, page).concatMap(new Function&lt;WatchList, ObservableSource&lt;? extends WatchList&gt;&gt;() {
          @Override
          public ObservableSource&lt;? extends WatchList&gt; apply(WatchList watchList) throws Exception {
              return Observable.fromArray(watchList);
          }
      });
  
  }
  </code></pre>
  
  <p>And in the same file the helper file similar to the previous SO post.</p>
  
  <pre><code>private Observable&lt;WatchList&gt; getWatchlistFromServerHelper(String sessionToken, String accountId, int page) {
          return mRestAPI.fetchWatchlistOnServer(sessionToken, accountId, page).concatMap(new Function&lt;WatchList, Observable&lt;WatchList&gt;&gt;() {
              @Override
              public Observable&lt;WatchList&gt; apply(WatchList watchList) throws Exception {
                  if (watchList.getPage() == watchList.getTotalPages()) {
                      return Observable.just(watchList);
                  }
                  return Observable.just(watchList).concatWith(getWatchlistFromServerHelper(sessionToken, accountId, page + 1));
              }
          });
      }
  </code></pre>
  
  <p>I'm calling this via a subscribe on my activity.</p>
  
  <pre><code>mSectionLogic.syncWatchlistSection(String.valueOf(account.getId()))
              .observeOn(AndroidSchedulers.mainThread())
              .subscribeOn(Schedulers.io())
              .subscribe(new Observer&lt;WatchList&gt;() {
                ... so on ...
  </code></pre>
  
  <p>This works but doesn't handle the paging. The response looks something like:</p>
  
  <pre><code>{
    page: 1,
    totalPages: 5,
    results: []
  }
  </code></pre>
  
  <p>What I've done is it should return when <code>page == totalPages</code>. Except it doesn't combine the previous results and just returns the last page of results.</p>
  
  <p>mRestAPI.fetchWatchlistOnServer is the Retrofit2 Get request that returns <code>Observable&lt;Watchlist&gt;</code>.</p>
  
  <p>What am I doing wrong? How do I combine results with Observable? Preferably not in Lamda notation.</p>
  
  <p>Other resources I looked at:
  - <a href="https://stackoverflow.com/questions/44509728/android-rxjava2-retrofit2-chaining-calls-with-pagination-token">android rxjava2/retrofit2 chaining calls with pagination token</a></p>