T- the type of item the Observer expects to observe
public interface Observer<T>
Observer is subscribed to an
ObservableSource through the
onSubscribe(Disposable) with a
Disposable that allows
disposing the sequence at any time, then the
ObservableSource may call the Observer's
onNext(T) method any number of times
to provide notifications. A well-behaved
ObservableSource will call an
onComplete() method exactly once or the
onError(java.lang.Throwable) method exactly once.
Observer's method must happen in a serialized fashion, that is, they must not
be invoked concurrently by multiple threads in an overlapping fashion and the invocation pattern must
adhere to the following protocol:
onSubscribe onNext* (onError | onComplete)?
Observer to multiple
ObservableSources is not recommended. If such reuse
happens, it is the duty of the
Observer implementation to be ready to receive multiple calls to
its methods and ensure proper concurrent behavior of its business logic.
The implementations of the
onXXX methods should avoid throwing runtime exceptions other than the following cases
(see Rule 2.13 of the Reactive Streams specification):
null, the methods can throw a
NullPointerException. Note though that RxJava prevents
nulls to enter into the flow and thus there is generally no need to check for nulls in flows assembled from standard sources and intermediate operators.
Violating Rule 2.13 results in undefined flow behavior. Generally, the following can happen:
ObservableSource.subscribe(Observer)throws instead of returning normally.
Executor) providing the asynchronous boundary the code is running and either routes the exception to the global
RxJavaPlugins.onError(Throwable)handler or the current thread's
Observable's perspective, an
Observeris the end consumer thus it is the
Observer's responsibility to handle the error case and signal it "further down". This means unreliable code in the
onXXXmethods should be wrapped into `try-catch`es, specifically in
onComplete(), and handled there (for example, by logging it or presenting the user with an error dialog). However, if the error would be thrown from
onNext(Object), Rule 2.13 mandates the implementation calls
Disposable.dispose()and signals the exception in a way that is adequate to the target context, for example, by calling
onError(Throwable)on the same
If, for some reason, the
Observer won't follow Rule 2.13, the
Observable.safeSubscribe(Observer) can wrap it
with the necessary safeguards and route exceptions thrown from
onError and route exceptions thrown
onComplete into the global error handler via
|Modifier and Type||Method and Description|
Notifies the Observer that the
Notifies the Observer that the
Provides the Observer with a new item to observe.
Provides the Observer with the means of cancelling (disposing) the connection (channel) with the Observable in both synchronous (from within
void onSubscribe(@NonNull Disposable d)
onNext(Object)) and asynchronous manner.
d- the Disposable instance whose
Disposable.dispose()can be called anytime to cancel the connection
Observable may call this method 0 or more times.
t- the item emitted by the Observable
Observablehas experienced an error condition.
e- the exception encountered by the Observable