0

I want to call a second request with the first Observable value as parameter. So I follow this order:

First, we emit a single value "a". Second, on doOnSuccess() method I create a Completable to simulate a heavy work on a new thread. I don't want this in the same thread, that's why I do this, but I am not sure if this is suitable in that case.

This code is working great but I want to know if there is another approach to achieve this easily because I know it's ugly

 Single.just("a")
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.computation())
                    .doOnSuccess(new Consumer<String>() {
                        @Override
                        public void accept(String s) throws Exception {
                            Completable.create(new CompletableOnSubscribe() {
                                @Override
                                public void subscribe(@NonNull CompletableEmitter e) throws Exception {
                                    //long http request
                                    Thread.sleep(2000);
                                    e.onComplete();
                                }
                            })
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribeOn(Schedulers.computation())
                            .subscribe(new DisposableCompletableObserver() {
                                @Override
                                public void onComplete() {
                                    Log.d("MainActivity", "onComplete second: ");

                                }

                                @Override
                                public void onError(@NonNull Throwable e) {

                                }
                            });
                        }
                    })
                    .subscribeWith(new DisposableSingleObserver<String>() {
                        @Override
                        public void onSuccess(@NonNull String s) {
                            Log.d("MainActivity", "onNext first: " + s);
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {

                        }
                    });
Jemshit
  • 9,501
  • 5
  • 69
  • 106
Deep
  • 189
  • 3

0 Answers0