11

I'm new to RxJava and RxAndroid and trying to understand the difference between Observable.just and Single. It looks like each is designed to emit one item for its observer.

Here is the code of my simple Android activity with two buttons. The first button creates an Observable, and the second button creates a Single:

findViewById(R.id.just).setOnClickListener(view -> Observable.just("item").subscribe(
        new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.d(LOG_TAG, "just onSubscribe");
            }

            @Override
            public void onNext(String s) {
                Log.d(LOG_TAG, "just s=" + s);
            }

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

            @Override
            public void onComplete() {
                Log.d(LOG_TAG, "just onComplete");
            }
        }));

findViewById(R.id.single).setOnClickListener(
        view -> Single.create((SingleOnSubscribe<String>) e -> {
        })
                .subscribe(new SingleObserver<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(LOG_TAG, "single onSubscribe");
                    }

                    @Override
                    public void onSuccess(String o) {
                        Log.d(LOG_TAG, "single onSuccess");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(LOG_TAG, "single onError", e);
                    }
                }));

When I press the "Just" button, onSubscribe, onNext, and onComplete are called.

When I press the "Single" button, only SingleObserver#onSubscibe is called, and SingleObserver#onSuccess is not.

The versions of RxJava and RxAndroid in my build.gradle:

compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
compile 'io.reactivex.rxjava2:rxjava:2.1.3'
Maksim Dmitriev
  • 5,985
  • 12
  • 73
  • 138

1 Answers1

3

Your code is working as expected. With the first you emit item but not in the second.

You need to change to

 findViewById(R.id.single).setOnClickListener(
    view -> Single.create((SingleOnSubscribe<String>) e -> {
            if(!e.isDisposed())
            e.onSuccess("item");
    })
            .subscribe(new SingleObserver<String>() {
                @Override
                public void onSubscribe(Disposable d) {
                    Log.d(LOG_TAG, "single onSubscribe");
                }

                @Override
                public void onSuccess(String o) {
                    Log.d(LOG_TAG, "single onSuccess" + " "+o);
                }

                @Override
                public void onError(Throwable e) {
                    Log.d(LOG_TAG, "single onError", e);
                }
            }));

Now you should see "item" in onSuccess.

Say you want to do some operation then return a string you would do as suggested above. Suppose your operation fails you can then do e.onError(new IOException());) , now you should see the error in onError

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • OK, thanks. But the bottom line is that they are both designed to emit one event, right? – Maksim Dmitriev Oct 12 '17 at 06:05
  • @MaksimDmitriev https://github.com/ReactiveX/RxJava/wiki/What%27s-different-in-2.0. check the docs i think the docs has a better explanation – Raghunandan Oct 12 '17 at 06:08
  • 1
    @MaksimDmitriev yes. and it depends on in what cases you need to use Observable/Flowable/Single/Maybe/Completable. Check this an example of usecase of RxJava with Room https://medium.com/google-developers/room-rxjava-acb0cd4f3757 – Raghunandan Oct 12 '17 at 06:15
  • 2
    The `Single.create()` javadoc even has an example: http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Single.html#create-io.reactivex.SingleOnSubscribe- – akarnokd Oct 12 '17 at 07:37