Interface Observer<T>
- Type Parameters:
T
- the type of item the Observer expects to observe
- All Known Implementing Classes:
AsyncSubject
,BasicFuseableObserver
,BehaviorSubject
,BlockingBaseObserver
,BlockingFirstObserver
,BlockingLastObserver
,BlockingObservableIterable.BlockingObservableIterator
,BlockingObservableLatest.BlockingObservableLatestIterator
,BlockingObservableMostRecent.MostRecentObserver
,BlockingObservableNext.NextObserver
,BlockingObserver
,CompletableAndThenObservable.AndThenObservableObserver
,CompletableFromObservable.CompletableFromObservableObserver
,ConcatMapXMainObserver
,DefaultObserver
,DeferredScalarObserver
,DisposableAutoReleaseObserver
,DisposableLambdaObserver
,DisposableObserver
,EmptyComponent
,FlowableFromObservable.SubscriberObserver
,ForEachWhileObserver
,FutureObserver
,InnerQueuedObserver
,LambdaObserver
,MaybeFlatMapObservable.FlatMapObserver
,ObservableAll.AllObserver
,ObservableAllSingle.AllObserver
,ObservableAmb.AmbInnerObserver
,ObservableAny.AnyObserver
,ObservableAnySingle.AnyObserver
,ObservableBuffer.BufferExactObserver
,ObservableBuffer.BufferSkipObserver
,ObservableBufferBoundary.BufferBoundaryObserver
,ObservableBufferBoundary.BufferBoundaryObserver.BufferOpenObserver
,ObservableBufferBoundary.BufferCloseObserver
,ObservableBufferExactBoundary.BufferBoundaryObserver
,ObservableBufferExactBoundary.BufferExactBoundaryObserver
,ObservableBufferTimed.BufferExactBoundedObserver
,ObservableBufferTimed.BufferExactUnboundedObserver
,ObservableBufferTimed.BufferSkipBoundedObserver
,ObservableCache
,ObservableCollect.CollectObserver
,ObservableCollectSingle.CollectObserver
,ObservableCollectWithCollector.CollectorObserver
,ObservableCollectWithCollectorSingle.CollectorSingleObserver
,ObservableCombineLatest.CombinerObserver
,ObservableConcatMap.ConcatMapDelayErrorObserver
,ObservableConcatMap.ConcatMapDelayErrorObserver.DelayErrorInnerObserver
,ObservableConcatMap.SourceObserver
,ObservableConcatMap.SourceObserver.InnerObserver
,ObservableConcatMapCompletable.ConcatMapCompletableObserver
,ObservableConcatMapEager.ConcatMapEagerMainObserver
,ObservableConcatMapMaybe.ConcatMapMaybeMainObserver
,ObservableConcatMapScheduler.ConcatMapDelayErrorObserver
,ObservableConcatMapScheduler.ConcatMapDelayErrorObserver.DelayErrorInnerObserver
,ObservableConcatMapScheduler.ConcatMapObserver
,ObservableConcatMapScheduler.ConcatMapObserver.InnerObserver
,ObservableConcatMapSingle.ConcatMapSingleMainObserver
,ObservableConcatWithCompletable.ConcatWithObserver
,ObservableConcatWithMaybe.ConcatWithObserver
,ObservableConcatWithSingle.ConcatWithObserver
,ObservableCount.CountObserver
,ObservableCountSingle.CountObserver
,ObservableDebounce.DebounceObserver
,ObservableDebounce.DebounceObserver.DebounceInnerObserver
,ObservableDebounceTimed.DebounceTimedObserver
,ObservableDelay.DelayObserver
,ObservableDelaySubscriptionOther.DelayObserver
,ObservableDelaySubscriptionOther.DelayObserver.OnComplete
,ObservableDematerialize.DematerializeObserver
,ObservableDetach.DetachObserver
,ObservableDistinct.DistinctObserver
,ObservableDistinctUntilChanged.DistinctUntilChangedObserver
,ObservableDoAfterNext.DoAfterObserver
,ObservableDoFinally.DoFinallyObserver
,ObservableDoOnEach.DoOnEachObserver
,ObservableElementAt.ElementAtObserver
,ObservableElementAtMaybe.ElementAtObserver
,ObservableElementAtSingle.ElementAtObserver
,ObservableFilter.FilterObserver
,ObservableFirstStageObserver
,ObservableFlatMap.InnerObserver
,ObservableFlatMap.MergeObserver
,ObservableFlatMapCompletable.FlatMapCompletableMainObserver
,ObservableFlatMapCompletableCompletable.FlatMapCompletableMainObserver
,ObservableFlatMapMaybe.FlatMapMaybeObserver
,ObservableFlatMapSingle.FlatMapSingleObserver
,ObservableFlatMapStream.FlatMapStreamObserver
,ObservableFlattenIterable.FlattenIterableObserver
,ObservableGroupBy.GroupByObserver
,ObservableGroupJoin.LeftRightEndObserver
,ObservableGroupJoin.LeftRightObserver
,ObservableHide.HideDisposable
,ObservableIgnoreElements.IgnoreObservable
,ObservableIgnoreElementsCompletable.IgnoreObservable
,ObservableLastMaybe.LastObserver
,ObservableLastSingle.LastObserver
,ObservableLastStageObserver
,ObservableMap.MapObserver
,ObservableMapNotification.MapNotificationObserver
,ObservableMapOptional.MapOptionalObserver
,ObservableMaterialize.MaterializeObserver
,ObservableMergeWithCompletable.MergeWithObserver
,ObservableMergeWithMaybe.MergeWithObserver
,ObservableMergeWithSingle.MergeWithObserver
,ObservableObserveOn.ObserveOnObserver
,ObservableOnErrorComplete.OnErrorCompleteObserver
,ObservableOnErrorNext.OnErrorNextObserver
,ObservableOnErrorReturn.OnErrorReturnObserver
,ObservablePublish.PublishConnection
,ObservablePublishSelector.SourceObserver
,ObservablePublishSelector.TargetObserver
,ObservableReduceMaybe.ReduceObserver
,ObservableReduceSeedSingle.ReduceSeedObserver
,ObservableRefCount.RefCountObserver
,ObservableRepeat.RepeatObserver
,ObservableRepeatUntil.RepeatUntilObserver
,ObservableRepeatWhen.RepeatWhenObserver
,ObservableRepeatWhen.RepeatWhenObserver.InnerRepeatObserver
,ObservableReplay.ReplayObserver
,ObservableRetryBiPredicate.RetryBiObserver
,ObservableRetryPredicate.RepeatObserver
,ObservableRetryWhen.RepeatWhenObserver
,ObservableRetryWhen.RepeatWhenObserver.InnerRepeatObserver
,ObservableSampleTimed.SampleTimedEmitLast
,ObservableSampleTimed.SampleTimedNoLast
,ObservableSampleTimed.SampleTimedObserver
,ObservableSampleWithObservable.SampleMainEmitLast
,ObservableSampleWithObservable.SampleMainNoLast
,ObservableSampleWithObservable.SampleMainObserver
,ObservableSampleWithObservable.SamplerObserver
,ObservableScan.ScanObserver
,ObservableScanSeed.ScanSeedObserver
,ObservableSequenceEqual.EqualObserver
,ObservableSequenceEqualSingle.EqualObserver
,ObservableSingleMaybe.SingleElementObserver
,ObservableSingleSingle.SingleElementObserver
,ObservableSingleStageObserver
,ObservableSkip.SkipObserver
,ObservableSkipLast.SkipLastObserver
,ObservableSkipLastTimed.SkipLastTimedObserver
,ObservableSkipUntil.SkipUntil
,ObservableSkipUntil.SkipUntilObserver
,ObservableSkipWhile.SkipWhileObserver
,ObservableStageObserver
,ObservableSubscribeOn.SubscribeOnObserver
,ObservableSwitchIfEmpty.SwitchIfEmptyObserver
,ObservableSwitchMap.SwitchMapInnerObserver
,ObservableSwitchMap.SwitchMapObserver
,ObservableSwitchMapCompletable.SwitchMapCompletableObserver
,ObservableSwitchMapMaybe.SwitchMapMaybeMainObserver
,ObservableSwitchMapSingle.SwitchMapSingleMainObserver
,ObservableTake.TakeObserver
,ObservableTakeLast.TakeLastObserver
,ObservableTakeLastOne.TakeLastOneObserver
,ObservableTakeLastTimed.TakeLastTimedObserver
,ObservableTakeUntil.TakeUntilMainObserver
,ObservableTakeUntil.TakeUntilMainObserver.OtherObserver
,ObservableTakeUntilPredicate.TakeUntilPredicateObserver
,ObservableTakeWhile.TakeWhileObserver
,ObservableThrottleFirstTimed.DebounceTimedObserver
,ObservableThrottleLatest.ThrottleLatestObserver
,ObservableTimeInterval.TimeIntervalObserver
,ObservableTimeout.TimeoutConsumer
,ObservableTimeout.TimeoutFallbackObserver
,ObservableTimeout.TimeoutObserver
,ObservableTimeoutTimed.FallbackObserver
,ObservableTimeoutTimed.TimeoutFallbackObserver
,ObservableTimeoutTimed.TimeoutObserver
,ObservableToList.ToListObserver
,ObservableToListSingle.ToListObserver
,ObservableUnsubscribeOn.UnsubscribeObserver
,ObservableUsing.UsingObserver
,ObservableWindow.WindowExactObserver
,ObservableWindow.WindowSkipObserver
,ObservableWindowBoundary.WindowBoundaryInnerObserver
,ObservableWindowBoundary.WindowBoundaryMainObserver
,ObservableWindowBoundarySelector.WindowBoundaryMainObserver
,ObservableWindowBoundarySelector.WindowBoundaryMainObserver.WindowEndObserverIntercept
,ObservableWindowBoundarySelector.WindowBoundaryMainObserver.WindowStartObserver
,ObservableWindowTimed.AbstractWindowObserver
,ObservableWindowTimed.WindowExactBoundedObserver
,ObservableWindowTimed.WindowExactUnboundedObserver
,ObservableWindowTimed.WindowSkipObserver
,ObservableWithLatestFrom.WithLatestFromObserver
,ObservableWithLatestFrom.WithLatestFromOtherObserver
,ObservableWithLatestFromMany.WithLatestFromObserver
,ObservableWithLatestFromMany.WithLatestInnerObserver
,ObservableZip.ZipObserver
,ObservableZipIterable.ZipIterableObserver
,ObserverResourceWrapper
,PublishSubject
,QueueDrainObserver
,ReplaySubject
,ResourceObserver
,SafeObserver
,SerializedObserver
,SerializedSubject
,SingleDelayWithObservable.OtherSubscriber
,SingleFlatMapObservable.FlatMapObserver
,Subject
,TestObserver
,TestObserver.EmptyObserver
,UnicastSubject
When an Observer
is subscribed to an ObservableSource
through the ObservableSource.subscribe(Observer)
method,
the ObservableSource
calls 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 Observer
's onComplete()
method exactly once or the Observer
's
onError(java.lang.Throwable)
method exactly once.
Calling the 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)?
Subscribing an Observer
to multiple ObservableSource
s 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.
Calling onSubscribe(Disposable)
, onNext(Object)
or onError(Throwable)
with a
null
argument is forbidden.
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):
- If the argument is
null
, the methods can throw aNullPointerException
. Note though that RxJava preventsnull
s 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. - If there is a fatal error (such as
VirtualMachineError
).
Violating Rule 2.13 results in undefined flow behavior. Generally, the following can happen:
- An upstream operator turns it into an
onError(java.lang.Throwable)
call. - If the flow is synchronous, the
ObservableSource.subscribe(Observer)
throws instead of returning normally. - If the flow is asynchronous, the exception propagates up to the component (
Scheduler
orExecutor
) providing the asynchronous boundary the code is running and either routes the exception to the globalRxJavaPlugins.onError(Throwable)
handler or the current thread'sThread.UncaughtExceptionHandler.uncaughtException(Thread, Throwable)
handler.
Observable
's perspective, an Observer
is 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 onXXX
methods should be wrapped into `try-catch`es, specifically in onError(Throwable)
or 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 Observer
instance.
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 onNext
into onError
and route exceptions thrown
from onError
and onComplete
into the global error handler via RxJavaPlugins.onError(Throwable)
.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Notifies theObserver
that theObservable
has finished sending push-based notifications.void
Notifies theObserver
that theObservable
has experienced an error condition.void
Provides theObserver
with a new item to observe.void
Provides theObserver
with the means of cancelling (disposing) the connection (channel) with theObservable
in both synchronous (from withinonNext(Object)
) and asynchronous manner.
-
Method Details
-
onSubscribe
Provides theObserver
with the means of cancelling (disposing) the connection (channel) with theObservable
in both synchronous (from withinonNext(Object)
) and asynchronous manner.- Parameters:
d
- theDisposable
instance whoseDisposable.dispose()
can be called anytime to cancel the connection- Since:
- 2.0
-
onNext
Provides theObserver
with a new item to observe.The
Observable
may call this method 0 or more times.The
Observable
will not call this method again after it calls eitheronComplete()
oronError(java.lang.Throwable)
.- Parameters:
t
- the item emitted by the Observable
-
onError
Notifies theObserver
that theObservable
has experienced an error condition.If the
Observable
calls this method, it will not thereafter callonNext(T)
oronComplete()
.- Parameters:
e
- the exception encountered by the Observable
-
onComplete
void onComplete()Notifies theObserver
that theObservable
has finished sending push-based notifications.The
Observable
will not call this method if it callsonError(java.lang.Throwable)
.
-