1

场景 我有一个区域列表,我想从服务器获取每个区域中的所有项目。所以,我决定用它来试试。我不知道我是不是“疯了”。这就是我所做的:BehaviorSubject

private Observable<ItemListResponse> getItemByTerritory() {

    List<String> territories = PrefUtils.getUserTerritories(context);

    BehaviorSubject<Integer> territoryControl = BehaviorSubject.<Integer>create(0);
    Observable<ItemListResponse> territoryRet = territoryControl.asObservable().concatMap(integer -> {

        if (integer != territories.size()) {

            BehaviorSubject<Integer> ret = BehaviorSubject.create(1);
            return ret.asObservable().concatMap(integer1 -> {

                if (integer > 0) {
                    return bakeryAPI.getBakeries("Bearer " + PrefUtils.getToken(context), integer1, 21, territories.get(integer))
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .doOnNext(itemListRespont -> {
                                if (itemListRespont.getItemResponse().getMeta().getPage() != itemListRespont.getItemResponse().getMeta().getPageCount() && itemListRespont.getItemResponse().getMeta().getPageCount() > 0) {
                                    ret.onNext(integer1 + 1);
                                } else {
                                    ret.onNext(-1);
                                    territoryControl.onNext(integer + 1);
                                }
                            });
                } else {
                    return Observable.<ItemListResponse>empty().doOnCompleted(ret::onCompleted);
                }
            });
        } else {
            return Observable.<ItemListResponse>empty().doOnCompleted(territoryControl::onCompleted);
        }
    });
    return Observable.defer(() -> territoryRet);
}

它所做的只是挂起,就像我这样做时根本没有运行

public void getItemByTerritory(APIRequestListener apiRequestListener){
    realm.executeTransaction(realm1 -> realm1.where(RealmItem.class).findAll().deleteAllFromRealm());

    getItemByTerritory().subscribe(new Subscriber< ItemListResponse >() {
        @Override
        public void onCompleted() {
            Log.e(TAG, "Completed Bakery");
            apiRequestListener.completed();
            unsubscribe();
        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
            apiRequestListener.handleError(e);
        }

        @Override
        public void onNext(ItemListResponse itemListRespont) {
        }
    });
}
4

1 回答 1

1

如果我理解正确并且您所追求的是获取getBakeries响应页面,那么以下递归函数调用应该可以解决问题:

private Observable<ItemListResponse> getItemByTerritory() {
    List<String> territories = PrefUtils.getUserTerritories(context);
    return Observable.from(territories).flatMap(territory -> fetchTerritoryPage(territory, 0));
}

private Observable<ItemListResponse> fetchTerritoryPage(String territory, int page) {
    System.out.println("Fetch territory page " + page);
    return bakeryAPI.getBakeries("Bearer " + PrefUtils.getToken(context), page, 21, territory).flatMap(response
            -> {
        final ItemListResponse.ItemResponse.Meta meta = response.getItemResponse().getMeta();
        final Observable<ItemListResponse> thisPage = Observable.just(response);
        if (page < meta.getPageCount()) {
            final Observable<ItemListResponse> nextPage = fetchTerritoryPage(territory, page + 1);
            return thisPage.concatWith(nextPage);
        } else {
            return thisPage;
        }
    });
}

请注意如何fetchTerritoryPage递归调用自身来获取下一页。

于 2016-10-13T19:10:53.950 回答