如何从 Single 的结果迭代 Flowable
How to iterate a Flowable from the result of a Single
我正在使用 Retrofit 和 Rxjava 来检索单个响应。
@GET("/commonapi/search")
Single<MyEntity> requestAddressRx(@Query("searchVal") String searchVal,
@Query("pageNum") int pageNum);
我有一个 json 将 return 以下
{"found":240,"totalNumPages":16,"pageNum":1,"results":[...
目前,要检索所有 16 页,我必须执行以下操作:-
- 调用 requestAddressRx() 一次以获得 totalNumPages 的第一个结果,
- 然后创建一个单独的 Flowable 调用 requestAddressRx() 从第 2 页循环到第 16 页。
有什么方法可以将第 1 步和第 2 步结合在一起吗?
//经过一番摸索-我目前拥有的是
Single<List<MyEntity>> result = addressRetrofitRx.requestAddressQueryRx(query, 1)
//now i get the number of pages from the results
// -NOTE: the result is now discarded :(
.map( r-> r.getTotalNumPages() )
//now i create a flowable from the number of pages
.flatMapPublisher(totalNumPages -> Flowable.range(1, totalNumPages))
.flatMapSingle(pageNumber -> addressRetrofitRx.requestAddressQueryRx(query, pageNumber))
.toList();
这导致Rxjava调用page1两次(第一次是我读取totalNumPages后被读取丢弃)
此外,如果结果只有 1 个 totalNumPages,我仍会生成 flowable。
应该有更好的方法来解决这个问题,但我似乎无法弄清楚。 JSON 在结果中对 numberOfPages 进行编码的模式非常常见,因此我假设有一种正确的 RxJava 方法来解析它们。
您不必计算出总页数,只需将第一页的 MyEntity
与其余页面连接起来即可:
Single<List<MyEntity>> result = api.requestAddressQueryRx(query, 1)
//now i create a flowable from the number of pages
.flatMapPublisher(r -> {
Flowable<MyEntity> pages = Flowable.just(r);
int maxPages = r.getTotalNumPages();
if (maxPages > 1) {
pages = pages
.concatWith(
Flowable.range(2, maxPages - 1)
.flatMapSingle(pageNumber ->
api.requestAddressQueryRx(query, pageNumber)
, /* eager error */ false, /* keep order of pages */ 1)
);
}
return pages;
})
.toList();
我正在使用 Retrofit 和 Rxjava 来检索单个响应。
@GET("/commonapi/search")
Single<MyEntity> requestAddressRx(@Query("searchVal") String searchVal,
@Query("pageNum") int pageNum);
我有一个 json 将 return 以下
{"found":240,"totalNumPages":16,"pageNum":1,"results":[...
目前,要检索所有 16 页,我必须执行以下操作:-
- 调用 requestAddressRx() 一次以获得 totalNumPages 的第一个结果,
- 然后创建一个单独的 Flowable 调用 requestAddressRx() 从第 2 页循环到第 16 页。
有什么方法可以将第 1 步和第 2 步结合在一起吗?
//经过一番摸索-我目前拥有的是
Single<List<MyEntity>> result = addressRetrofitRx.requestAddressQueryRx(query, 1)
//now i get the number of pages from the results
// -NOTE: the result is now discarded :(
.map( r-> r.getTotalNumPages() )
//now i create a flowable from the number of pages
.flatMapPublisher(totalNumPages -> Flowable.range(1, totalNumPages))
.flatMapSingle(pageNumber -> addressRetrofitRx.requestAddressQueryRx(query, pageNumber))
.toList();
这导致Rxjava调用page1两次(第一次是我读取totalNumPages后被读取丢弃)
此外,如果结果只有 1 个 totalNumPages,我仍会生成 flowable。
应该有更好的方法来解决这个问题,但我似乎无法弄清楚。 JSON 在结果中对 numberOfPages 进行编码的模式非常常见,因此我假设有一种正确的 RxJava 方法来解析它们。
您不必计算出总页数,只需将第一页的 MyEntity
与其余页面连接起来即可:
Single<List<MyEntity>> result = api.requestAddressQueryRx(query, 1)
//now i create a flowable from the number of pages
.flatMapPublisher(r -> {
Flowable<MyEntity> pages = Flowable.just(r);
int maxPages = r.getTotalNumPages();
if (maxPages > 1) {
pages = pages
.concatWith(
Flowable.range(2, maxPages - 1)
.flatMapSingle(pageNumber ->
api.requestAddressQueryRx(query, pageNumber)
, /* eager error */ false, /* keep order of pages */ 1)
);
}
return pages;
})
.toList();