用rxjava2链接多个流

时间:2019-06-20 07:39:18

标签: android rx-java2 rx-android chaining

我尝试通过RxJava2编写代码调用服务API。

我有多个API(包括循环调用API)。 这是我想要的示例代码。

我的问题是为什么onComplete没打电话?


Observable.just("test")
                .flatMapCompletable(new Function<String, CompletableSource>() {
                    @Override
                    public CompletableSource apply(String s) throws Exception {
                        ArrayList<Observable<Void>> arrayList = new ArrayList<>();
                        arrayList.add(Completable.complete().delay(3, TimeUnit.SECONDS).andThen(new Observable<Void>() {
                            @Override
                            protected void subscribeActual(Observer<? super Void> observer) {
                                Log.e("subscribeActual", "onComplete1");
                                observer.onComplete();
                            }
                        }));
                        arrayList.add(Completable.complete().delay(1, TimeUnit.SECONDS).andThen(new Observable<Void>() {
                            @Override
                            protected void subscribeActual(Observer<? super Void> observer) {
                                Log.e("subscribeActual", "onComplete2");
                                observer.onComplete();
                            }
                        }));
                        arrayList.add(Completable.complete().delay(2, TimeUnit.SECONDS).andThen(new Observable<Void>() {
                            @Override
                            protected void subscribeActual(Observer<? super Void> observer) {
                                Log.e("subscribeActual", "onComplete3");
                                observer.onComplete();
                            }
                        }));
                        return Observable.merge(arrayList)
                                .toList()
                                .flatMapCompletable(new Function<List<Void>, CompletableSource>() {
                                    @Override
                                    public CompletableSource apply(List<Void> voids) throws Exception {
                                        return Completable.complete();
                                    }
                                });
                    }
                })
                .andThen(new Observable<Void>() {
                    @Override
                    protected void subscribeActual(Observer<? super Void> observer) {
                        Log.e("subscribeActual", "subscribeActual");
                        observer.onNext(null);
                    }
                })
                .flatMap(new Function<Void, ObservableSource<String>>() {
                    @Override
                    public ObservableSource<String> apply(Void aVoid) throws Exception {
                        Log.e("ObservableSource", "apply");
                        return Observable.just("Hello");
                    }
                })
                .flatMapCompletable(new Function<String, CompletableSource>() {
                    @Override
                    public CompletableSource apply(String s) throws Exception {
                        Log.e("apply", s);
                        return Completable.complete();
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CompletableObserver() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onComplete() {
                        Log.e("onComplete", "onComplete");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("onError", "onError " + e.getMessage());
                    }
                });

Log的结果是

  

subscribeActual:onComplete2

     

subscribeActual:onComplete3

     

subscribeActual:onComplete1

     

subscribeActual:subscribeActual

     

ObservableSource:应用

     

应用:您好

我希望在订阅(已完成或Observable<Void>)之前以“无效”链接可观察的结尾。

0 个答案:

没有答案