- Learning RxJava
- Thomas Nield
- 353字
- 2021-07-02 22:22:51
The Observer interface
The onNext(), onComplete(), and onError() methods actually define the Observer type, an abstract interface implemented throughout RxJava to communicate these events. This is the Observer definition in RxJava shown in the code snippet. Do not bother yourself about onSubscribe() for now, as we will cover it at the end of this chapter. Just bring your attention to the other three methods:
package io.reactivex;
import io.reactivex.disposables.Disposable;
public interface Observer<T> {
void onSubscribe(Disposable d);
void onNext(T value);
void onError(Throwable e);
void onComplete();
}
Observers and source Observables are somewhat relative. In one context, a source Observable is where your Observable chain starts and where emissions originate. In our previous examples, you could say that the Observable returned from our Observable.create() method or Observable.just() is the source Observable. But to the filter() operator, the Observable returned from the map() operator is the source. It has no idea where the emissions are originating from, and it just knows that it is receiving emissions from the operator immediately upstream from it, which come from map().
Conversely, each Observable returned by an operator is internally an Observer that receives, transforms, and relays emissions to the next Observer downstream. It does not know whether the next Observer is another operator or the final Observer at the end of the chain. When we talk about the Observer, we are often talking about the final Observer at the end of the Observable chain that consumes the emissions. But each operator, such as map() and filter(), also implements Observer internally.
We will learn in detail about how operators are built in Chapter 9, Transformers and Custom Operators. For now, we will focus on using an Observer for the subscribe() method.
- Learning LibGDX Game Development(Second Edition)
- CentOS 7 Server Deployment Cookbook
- Learning C++ Functional Programming
- Visual Basic程序設計教程
- Java Web應用開發技術與案例教程(第2版)
- 云計算通俗講義(第3版)
- Learning OpenCV 3 Computer Vision with Python(Second Edition)
- RealSenseTM互動開發實戰
- 一本書講透Java線程:原理與實踐
- SQL Server 2016 從入門到實戰(視頻教學版)
- 官方 Scratch 3.0 編程趣味卡:讓孩子們愛上編程(全彩)
- 計算機組裝與維護(第二版)
- Android初級應用開發
- Java 7 Concurrency Cookbook
- C語言編程魔法書:基于C11標準