Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Observable which does not pass anything in onNext()

Tags:

java

rx-java

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();              }          }) 
like image 240
Oliver Hausler Avatar asked Oct 01 '14 16:10

Oliver Hausler


People also ask

When the onNext () method of the subscriber is called?

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.

What is Observable in kotlin?

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.

How do I extract an object from Observable?

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.

What is an Observable in Rxjava?

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.


1 Answers

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.

like image 124
Miguel Avatar answered Sep 20 '22 05:09

Miguel