官术网_书友最值得收藏!

Subscribers - the Observer interface

The Subscriber from RxKotlin 1.x,  essentially became an Observer in RxKotlin 2.x. There is an Observer interface in RxKotlin 1.x, but Subscriber is what you pass to the subscribe() method, and it implements Observer. However, In RxJava 2.x, Subscriber only exists when talking about Flowables, which we will cover in Chapter 4Introduction to Backpressure and Flowables.

As you can see in the previous examples in this chapter, Observer is an interface with four methods in it—onNext(item:T), onError(error:Throwable), onComplete(), and onSubscribe(d:Disposable). As stated earlier, when we connect Observable to Observer, it looks for these four methods in Observer and calls them. So, the following is a short description of the four methods:

  • onNextObservable calls this method of Observer to pass each of the items one by one.
  • onComplete: When Observable wants to denote, it's done with passing items to the onNext method, and it calls the onComplete method of Observer.
  • onError: When Observable faces any error, it calls the onError method to deal with the error if defined in the Observer, otherwise, it throws the exception.
  • onSubscribe: This method is called whenever a new Observable subscribes to the Observer.
主站蜘蛛池模板: 龙陵县| 施甸县| 伊川县| 南召县| 崇阳县| 沭阳县| 马公市| 盐源县| 阿鲁科尔沁旗| 原阳县| 沙田区| 临武县| 南昌县| 庆阳市| 通榆县| 博罗县| 红河县| 大名县| 聊城市| 普定县| 舟曲县| 徐汇区| 平昌县| 江都市| 无锡市| 长泰县| 池州市| 塔河县| 大石桥市| 漳州市| 托克托县| 姜堰市| 平罗县| 丰都县| 海宁市| 蛟河市| 吐鲁番市| 延边| 沧州市| 尼木县| 奉新县|