2016-05-04 12 views
3

Retrofitレスポンスの処理方法はわかっていますが、rx javaを使用したREST APIのページング処理に問題があります。私が使用しているRetrofit 2とRxJavaでページ分割を処理する方法

背景

のREST APIは私に、ヘッダーの次のページへのリンクを次の応答が得られます。

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ページ目のリクエストを行いますが、レスポンスに「次の」ヘッダーリンクがなくなるまでこれらのページングリクエストを作成する方法がわからないので、すべてのリクエストをデータベースに保存できます。

このような要求を処理する方法についてのご意見はありますか?たぶん.takeUntil演算子?

答えて

-2

再帰のビットでconcatMapを使用できます。

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"))))); 
} 
+0

これは私が今まで見た中で最も難しいコードです! –

関連する問題