如何使用Retrofit 2和RxJava处理分页 [英] How to handle pagination with Retrofit 2 and RxJava

查看:454
本文介绍了如何使用Retrofit 2和RxJava处理分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何处理翻新响应,但是在使用带有rx java的REST API处理分页时遇到问题.

I know how to handle Retrofit responses but I have a problem with handling pagination from a REST API with rx java.

背景

我正在使用的rest api给我以下响应,并带有标题中下一页的链接:

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

HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Link: <http://some-endpoint.com/pictures?page=2>; 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
            ]
        }
    },
...
}

正确地知道我使用以下代码成功将分页请求发送到了第2页,但是在响应上没有"next"标题链接之前,我不知道如何发出这些分页请求,因此我可以保存所有请求到数据库的所有数据.

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.

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

关于如何处理此类请求的任何建议?也许.takeUntil运算符?

Any suggestions on how to handle this kind of requests? Maybe .takeUntil operator?

推荐答案

您可以使用concatMap进行一些递归.

You could use concatMap with a bit of recursion.

Observable<List<PointOfSaleEntity>> getPointsOfSalePaginateUntilEnd(String link) {
  if (link.equals("")) {
      return Observable.empty();
  } else {      
      return mApiService.getPointsOfSalesPaginate(link)                     
                        .concatMap(listResponse -> Observable.concat(Observable.just(listResponse),mApiService.getPointsOfSalesPaginate(NetworkUtil.getNextLinkPaginate(listResponse.headers().get("Link")))));
  }
}

Observable<List<PointOfSaleEntity>> getPointsOfSalePaginateUntilEnd() {   
       return mApiService.mApiService.getPointsOfSales()                        
                         .concatMap(listResponse -> Observable.concat(Observable.just(listResponse),mApiService.getPointsOfSalesPaginate(NetworkUtil.getNextLinkPaginate(listResponse.headers().get("Link")))));
}

这篇关于如何使用Retrofit 2和RxJava处理分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆