RxJava observable 嵌套问题

发布于 2022-09-11 17:11:24 字数 4865 浏览 14 评论 0

问题:
RxView.clicks 之后请求网络,使用retrofit,如下:

RxView.clicks(findViewById(R.id.button2))
                .subscribe(new Consumer<Unit>() {
                    @Override
                    public void accept(Unit unit) throws Exception {
                        Http.get().create(TimeApi.class).getLicense(TimeApi.url)
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Consumer<TimeModel>() {
                                    @Override
                                    public void accept(TimeModel timeModel) throws Exception {
                                        btn.setText(timeModel.getData() + "");
                                    }
                                }, new Consumer<Throwable>() {
                                    @Override
                                    public void accept(Throwable throwable) throws Exception {

                                    }
                                });
                    }
                });
                

怎么消除嵌套?
我使用了concatMap、flatMap,compose,都有一些问题。比如Scheduler问题,或者click事件不点击直接执行问题:


        ObservableTransformer<Unit, TimeModel> transformer = new ObservableTransformer<Unit, TimeModel>() {

            @Override
            public ObservableSource<TimeModel> apply(Observable upstream) {
                return Http.get().create(TimeApi.class).getLicense(TimeApi.url);
            }
        };

        RxView
                .clicks(btn2)
                .compose(transformer)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<TimeModel>() {
                    @Override
                    public void accept(TimeModel model) throws Exception {
                        Log.d("HHHH", model.getData()+"");
                        btn2.setText(String.valueOf(model.getData()));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Log.d("HHHH", "error");
                        btn2.setText("---------------"+throwable.getMessage());
                    }
                });

        RxView.clicks(btn2)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .concatMap(new Function<Unit, ObservableSource<TimeModel>>() {
                    @Override
                    public ObservableSource<TimeModel> apply(Unit unit) throws Exception {
                        return Http.get().create(TimeApi.class).getLicense(TimeApi.url);
                    }
                })
                .subscribe(new Consumer<TimeModel>() {
                    @Override
                    public void accept(TimeModel model) throws Exception {
                        Log.d("HHHH", model.getData()+"");
                        btn2.setText(String.valueOf(model.getData()));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Log.d("HHHH", "error");
                        btn2.setText("---------------"+throwable.getMessage());
                    }
                });


        Disposable subscribe = RxView
                .clicks(btn2)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap(new Function<Unit, ObservableSource<TimeModel>>() {
                    @Override
                    public ObservableSource<TimeModel> apply(Unit unit) throws Exception {
                        Log.d("HHHH", "request");
                        return Http.get().create(TimeApi.class).getLicense(TimeApi.url);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<TimeModel>() {
                    @Override
                    public void accept(TimeModel model) throws Exception {
                        Log.d("HHHH", model.getData()+"");
                        btn2.setText(String.valueOf(model.getData()));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Log.d("HHHH", "error");
                        btn2.setText("---------------"+throwable.getMessage());
                    }
                });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文