How to handle pagination with Retrofit 2 and RxJava
<p>I know how to handle Retrofit responses but I have a problem with handling pagination from a REST API with rx java.</p>

<p><strong>Background</strong></p>

<p>The rest api that I'm using gives me the following response with the link to the next page in the headers:</p>

<pre><code>HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Link: &lt;http://some-endpoint.com/pictures?page=2&gt;; rel="next"
Vary: Accept
X-Total-Count: 80

[
    {
        "id": 3,
        "url": "",
        "picture": {
            "id": 6,
            "url": "",
            "name": "Chrysler"
        },
        "location": {
            "type": "Point",
            "coordinates": [
                10.108956,
                41.389576000000005
            ]
        }
    },
    {
        "id": 4,
        "url": "",
        "picture": {
            "id": 26,
            "url": "",
            "name": "Douglas Aircraft"
        },
        "location": {
            "type": "Point",
            "coordinates": [
                10.1977759999999997,
                41.426645
            ]
        }
    },
...
}
</code></pre>

<p>Right know I managed to get the pagination request to page 2 with the following code but I don't know how to make those pagination requests until there is no "next" header link on the response so I can be able to save all data from all requests to the database.</p>

<pre><code>private Observable&lt;List&lt;PictureEntity&gt;&gt; getPictures(final LocationEntity locationEntity) {
    if (getDataFromServer()) {
        return mApiService.getPictures()
                .concatMap(new Func1&lt;Response&lt;List&lt;Picture&gt;&gt;, Observable&lt;Response&lt;List&lt;Picture&gt;&gt;&gt;&gt;() {
                    @Override
                    public Observable&lt;Response&lt;List&lt;Picture&gt;&gt;&gt; call(Response&lt;List&lt;Picture&gt;&gt; listResponse) {
                        String link = NetworkUtil.getNextLinkPaginate(listResponse.headers().get("Link"));
                        Timber.i(link);
                        return Observable.just(listResponse)
                                .concatWith(mApiService.getPicturesPaginate(link))
                                .reduce(new Func2&lt;Response&lt;List&lt;Picture&gt;&gt;, Response&lt;List&lt;Picture&gt;&gt;,
                                        Response&lt;List&lt;Picture&gt;&gt;&gt;() {
                                    @Override
                                    public Response&lt;List&lt;Picture&gt;&gt; call(Response&lt;List&lt;Picture&gt;&gt; listResponse,
                                                                            Response&lt;List&lt;Picture&gt;&gt; listResponse2) {
                                        listResponse.body().addAll(listResponse2.body());
                                        return listResponse;
                                    }
                                });
                    }
                })
                .concatMap(new Func1&lt;Response&lt;List&lt;Picture&gt;&gt;, Observable&lt;List&lt;PictureEntity&gt;&gt;&gt;() {
                    @Override
                    public Observable&lt;List&lt;PictureEntity&gt;&gt; call(Response&lt;List&lt;Picture&gt;&gt; listResponse) {
                        List&lt;PictureEntity&gt; pictureEntities =
                                mPictureDataMapper.transformToEntity(listResponse.body());
                        mDatabaseHelper.setPictureEntity(pictureEntities).subscribe();
                        return mDatabaseHelper.getNearbyPicturesEntity(locationEntity);
                    }
                });
    } else {
        return mDatabaseHelper.getNearbyPicturesEntity(locationEntity);
    }
}
</code></pre>

<p>Any suggestions on how to handle this kind of requests? Maybe .takeUntil operator?</p>