How Observable works

As we stated earlier, an Observable has three most important events/methods; let's discuss them one by one:

  • onNext: Observable passes all items one by one to this method.
  • onComplete: When all items have gone through the onNext method, Observable calls the onComplete method.
  • onError: When Observable faces any error, it calls the onError method to deal with the error, if defined. Note that both onError and onComplete are terminal events, and if onError is called, then it would never call onComplete and vice versa.
One thing to note here, the item in Observable that we are talking about can be anything; it is defined as Observable<T>, where T can be any class; even an array/list can be assigned as an Observable.

Let's ...

Get Reactive Programming in Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.