RxJava - 嵌套的Observables? (改型)

时间:2016-09-04 19:28:00

标签: java android retrofit rx-java rx-android

我面临的问题是我需要一个身份验证令牌来创建我的Retrofit服务。我目前使用Observable获取所述令牌,导致一个相当难看的Observable构造:

Observable<MyService> observable = application.getMyService();
    observable.observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(application.defaultSubscribeScheduler())
            .subscribe(new Subscriber<MyService>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    Log.e(TAG, "Error creating service: ", e);
                }

                @Override
                public void onNext(MyService myService) {
                    subscription = myService.searchStuff(searchFor)
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribeOn(application.defaultSubscribeScheduler())
                            .subscribe(new Subscriber<AResponseWrapper>() {
                                @Override
                                public void onCompleted() {

                                }

                                @Override
                                public void onError(Throwable error) {
                                    Log.e(TAG, "Error loading stuff: ", error);
                                }

                                @Override
                                public void onNext(AResponseWrapper wrapper) {
                                    MainPresenter.this.stuff = wrapper.getStuff();
                                }
                            });
                }
            });

我无法帮助,但觉得这不是应该怎么做的。我是对的吗?

1 个答案:

答案 0 :(得分:1)

我正在寻找Observable.flatMap。 它允许将结果映射到另一个可观察对象:

Observable<MyService> observable = application.getMyService();
    subscription = observable
            .observeOn(application.defaultSubscribeScheduler())
            .subscribeOn(application.defaultSubscribeScheduler())
            .flatMap(service -> service.searchStuff(searchFor))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<AResponseWrapper>() {
                @Override
                public void onCompleted() {
                    if (series.size() < 1) {
                        mainView.showMessage(R.string.no_stuff_found);
                    } else {
                        mainView.showStuff(stuff);
                    }
                }

                @Override
                public void onError(Throwable error) {
                    Log.e(TAG, "Error loading stuff: ", error);
                }

                @Override
                public void onNext(AResponseWrapper wrapper) {
                    MainPresenter.this.stuff= wrapper.getStuff();
                }
            });

请注意,我首先在IO Scheduler上观察并且只在flatMap之后我将在主线程上订阅。否则service.searchStuff调用(至少我认为是那部分)将在主线程上执行,产生NetworkOnMainThreadException

感谢@ ahmed-ashraf-g,他向我指出了answer