2

I'm new to RxJava. I have a code like this. I'm creating an observable from a string returned from repository.getStatus(). if it is null , I have to proceed with the proceed method without doing anything. If it is not null , I have to call repository.init() and then proceed. Here's what I have done.

Flowable.just(repository.getStatus()) // getStatus return a string , which can be null
        .doOnError(throwable -> proceed())
        .flatMapCompletable(s -> repository.init())
        .observeOn(Schedulers.io())
        .subscribeOn(AndroidSchedulers.mainThread())
        .subscribe(new DisposableCompletableObserver() {
                       @Override
                       public void onComplete() {
                           proceed();
                       }

                       @Override
                       public void onError(@NonNull Throwable e) {
                            handleErrors(t));
                       }
                  });

private void proceed(){
    // 
}

In case repository.getStatus() is null , wont it call the doOnError(throwable -> proceed())? Now it is crashing when repository.getStatus() is null. What is the best Rx way to deal with this kind of a usecase?

3
  • check this Commented Oct 10, 2017 at 10:21
  • @masp but as told in the accepted answer , Optional.ofNullable required API 24 above. My min version is 19 Commented Oct 10, 2017 at 10:35
  • Apart from the answer there is also a link in the question to a conversation from which you can understand a lot. Commented Oct 10, 2017 at 11:46

2 Answers 2

1

Since you're using Rx2, you can use the new Maybe type, which is the streaming equivalent of Optional. Unfortunately, it does not have a fromNullable factory method (see https://github.com/ReactiveX/RxJava/issues/5019), but you can use this:

Maybe.fromCallable(() -> repository.getStatus())

Sign up to request clarification or add additional context in comments.

Comments

0

Instead of piping repository.getStatus() with repository.init() , you could just create an observable from any of these two based on your condition.

Try something like this

    Flowable.just(repository.getStatus() == null ? Completable.complete() : repository.init())
            .flatMapCompletable(c -> c)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new DisposableCompletableObserver() {
                @Override
                public void onComplete() {
                    proceed();
                }

                @Override
                public void onError(@NonNull Throwable e) {
                     handleErrors(t));
                }
            });

You could even make your repository.getStatus() as a MayBe and do something like this

Maybe.create((MaybeOnSubscribe<String>) e -> {
        if (repository.getStatus() != null) e.onSuccess(repository.getStatus());
        e.onComplete();
    }).flatMapCompletable(s -> Completable.fromAction(() -> repository.init()))
            .subscribe(new DisposableCompletableObserver() {
                @Override
                public void onComplete() {
                    proceed();
                }

                @Override
                public void onError(@NonNull Throwable e) {
                    handleErrors(t));
                }
            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.