...

/

Observable and Observer

Observable and Observer

Learn about the various methods offered by Observables and Observers.

We'll cover the following...

An Observable can emit any number of items. As mentioned, to receive or listen to these emitted items, an Observer needs to subscribe to the Observable. The .subscribe() method is defined by the ObservableSource interface, which is implemented by Observable.

Press + to interact
public interface ObservableSource<T> {
void subscribe(Observer<? super T> observer);
}

Once the Observable and Observer have been paired via the .subscribe() method, the Observer receives the following events through its defined methods:

  • The .onSubscribe() method is called along with a Disposable object that may be used to unsubscribe from the Observable to stop receiving items.
  • The .onNext() method is called when an item is emitted by the Observable.
  • The .onComplete() method is called when the Observable has finished sending items.
  • The .onError() method is called when an error is encountered within the Observable. The specific error is passed to this method.
Press + to interact
public interface Observer<T> {
void onSubscribe(Disposable d);
void onNext(T value);
void onError(Throwable e);
void onComplete();
}

.Next()

The method .onNext() can be invoked zero, one, or multiple times by the Observable whereas the .onComplete() and ...