Observable which does not pass anything in onNext()
RxJava 2 Wiki:
RxJava 2.x no longer accepts null values and the following will yield NullPointerException immediately or as a signal to downstream.
...
This means that
Observable<Void>
can no longer emit any values but only terminate normally or with an exception. API designers may instead choose to defineObservable<Object>
with no guarantee on what Object will be (which should be irrelevant anyway)
It means that you can't use Void
and do Observable.just(null)
.
Use Object
or some other simple type instead:
Observable.just(new Object());
You don't need to call onNext
if your Observable
doesn't emit anything.
You could use Void
in your signature and do something like
Observable<Void> o = Observable.create(new Observable.OnSubscribe<Void>() {
@Override
public void call(Subscriber<? super Void> subscriber) {
// Do the work and call onCompleted when you done,
// no need to call onNext if you have nothing to emit
subscriber.onCompleted();
}
});
o.subscribe(new OnCompletedObserver<Void>() {
@Override
public void onCompleted() {
System.out.println("onCompleted");
}
@Override
public void onError(Throwable e) {
System.out.println("onError " + e.getMessage());
}
});
You can define an OnCompletedObserver to simplify your Observer callback so that you don't have to override the onNext since you don't need it.
public abstract class OnCompletedObserver<T> implements Observer<T> {
@Override
public void onNext(T o) {
}
}
If I've understood what you're asking then this should do the trick.
If you need something to be passed to onNext()
before onCompleted()
is called:
Observable.<Void>just(null)
If you only need onCompleted()
to be called:
Observable.empty()