I would need an Observable, for example to provide a system clock, which does not need to pass anything in onNext(). I couldn't find a signature that would allow me to do that.
Sure, I could use any object and then pass null, but that doesn't make much sense. So my question is if there is a better way to do that.
Observable.create(new Observable.OnSubscribe<Anyobject>() { // use any object in the signature @Override public void call(Subscriber<? super Anyobject> subscriber) { subscriber.onNext(null); // then pass null subscriber.onCompleted(); } })
After a Subscriber calls an Observable 's subscribe method, the Observable calls the Subscriber's Observer. onNext(T) method to emit items. A well-behaved Observable will call a Subscriber's Observer. onCompleted() method exactly once or the Subscriber's Observer.
An observable object can have one or more observers. An observer may be any object that implements interface Observer . After an observable instance changes, an application calling the Observable 's notifyObservers method causes all of its observers to be notified of the change by a call to their update method.
You cannot 'extract' something from an observable. You get items from observable when you subscribe to them (if they emit any). Since the object you are returning is of type Observable, you can apply operators to transform your data to your linking.
An Observable is like a speaker that emits the value. It does some work and emits some values. An Operator is like a translator which translates/modifies data from one form to another form. An Observer gets those values.
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With