MarcusH
MarcusH

Reputation: 1703

empty observable subscribed, but onNext not called?

I want an Observable that doesnt do anything except that when subscribed to, the observer's onNext callback is invoked. I think I found that with Observable.empty(), but the following does not result in the observer's onNext callback being invoked:

Observable<Void> emptyObservable = Observable.empty();
emptyObservable.subscribe(passedinObserver);

passedInObserver was defined:

Observer<Void> passedinObserver = new Observer<Void> {
        @Override public void onCompleted() {

        }

        @Override public void onError(Throwable e) {

        }

        @Override public void onNext(Void aVoid) {
            Log.d("onNext called");
        }
};

Upvotes: 1

Views: 2479

Answers (4)

Enigmativity
Enigmativity

Reputation: 117027

Try Observable.just(...). It will return one value and thus call .onNext(...).

Upvotes: 0

Dave Sexton
Dave Sexton

Reputation: 2652

Alternatively to the other answers, since empty only calls onCompleted without calling onNext, maybe you just want to do your work in onCompleted rather than in onNext?

Upvotes: 1

akarnokd
akarnokd

Reputation: 69997

You can use Observable.just(null) or any value since you are going to ingore the value anyway. However, it appears you want to perform additional work when values travel through an observable sequence, so you can use doOnNext():

Observable.range(1, 10)
.doOnNext(v -> Log.d("onNext called"))
.map(v -> v * v)
.subscribe(System.out::println);

Upvotes: 3

MarcusH
MarcusH

Reputation: 1703

onNext doesn't get called, onCompleted does for this type of Observable.

Upvotes: 1

Related Questions