RxAndroid - 1响应从多个可观察对象



我正在尝试学习rxandroid,所以问题就像标题。

1多个观测值的响应

我想从这2个请求中得到1个响应。

或者选项2我也想知道如何同步它们

在第一个请求完成后执行第二个请求。

示例代码:

 Observable<List<CategoriesTreeModel>> categoriesTreeObservable = ApiManager.getInstanceApi().getCategoriesTree();
         Subscription treeSubscription = categoriesTreeObservable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<CategoriesTreeModel>>() {
                    @Override
                    public void onCompleted() {
                        Log.e("CATALOG", "getCategoriesTree() complete");
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e("CATALOG", "getCategoriesTree() error");
                    }
                    @Override
                    public void onNext(List<CategoriesTreeModel> categoriesTreeModels) {
                        Log.e("CATALOG", "getCategoriesTree() next");
                    }
                });

        Observable<ResponseModel<CategoriesResponse>> categoriesObservable = ApiManager.getInstanceApi().getCategories();
        Subscription subscription = categoriesObservable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResponseModel<CategoriesResponse>>() {
                    @Override
                    public void onCompleted() {
                         Log.e("CATALOG", "getCategories() complete");
                    }
                    @Override
                    public void onError(Throwable e) {
                        Log.e("CATALOG", "getCategories() error");
                    }
                    @Override
                    public void onNext(ResponseModel<CategoriesResponse> categoriesResponseResponseModel) {
                        Log.e("CATALOG", "getCategories() onNext");

                    }
                });

编辑:这是一个正确的方法吗?

 Observable.zip(categoriesObservable, categoriesTreeObservable, new Func2<ResponseModel<CategoriesResponse>, List<CategoriesTreeModel>, Object>() {
            @Override
            public Object call(ResponseModel<CategoriesResponse> categoriesResponseResponseModel, List<CategoriesTreeModel> categoriesTreeModels) {
                for (int i = 0; i < categoriesResponseResponseModel.getList().size(); i++){
                    Log.e("RESPONSE", "CATEGORIES: " + categoriesResponseResponseModel.getList().get(i).getCategory_id() + " NAME: " + categoriesResponseResponseModel.getList().get(i).getTranslations().getPl_PL().getName());
                }
                for (int i = 0; i < categoriesTreeModels.size(); i++){
                    Log.e("RESPONSE", "TREE: ID " + categoriesTreeModels.get(i).getId() + " CHILD: " + (categoriesTreeModels.get(i).getChildren().size() > 0 ? " has children " : "no child"));
                }
                return null;
            }
        })
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe();

阅读一些关于MVP模式的内容,以便将数据逻辑与视图逻辑分开。"第一个请求完成后再做第二个请求"最直接的解决方案如下。这个模式不是世界上最好的,但至少它会给你一些代码参考。

final Observable<String> firstObservable = Observable.just("A")
        .delay(5, TimeUnit.SECONDS)
        .doOnNext(new Action1<String>() {
            @Override
            public void call(String s) {
                Log.d(TAG, "call: " + s);
            }
        });
firstObservable
        .subscribe();
final Observable<String> secondObservable = firstObservable
        .flatMap(new Func1<String, Observable<String>>() {
            @Override
            public Observable<String> call(String s) {
                return Observable.just(s + "B").delay(2, TimeUnit.SECONDS); // Make some api call
            }
        });
secondObservable.subscribe(new Action1<String>() {
    @Override
    public void call(String s) {
        Log.d(TAG, "call: " + s);
    }
});

相关内容

  • 没有找到相关文章

最新更新