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
public interface Observer<@NonNull T>
Provides a mechanism for receiving push-based notifications.When an
Observer
is subscribed to anObservableSource
through theObservableSource.subscribe(Observer)
method, theObservableSource
callsonSubscribe(Disposable)
with aDisposable
that allows disposing the sequence at any time, then theObservableSource
may call the Observer'sonNext(T)
method any number of times to provide notifications. A well-behavedObservableSource
will call anObserver
'sonComplete()
method exactly once or theObserver
'sonError(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 multipleObservableSource
s is not recommended. If such reuse happens, it is the duty of theObserver
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)
oronError(Throwable)
with anull
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, anObserver
is the end consumer thus it is theObserver
's responsibility to handle the error case and signal it "further down". This means unreliable code in theonXXX
methods should be wrapped into `try-catch`es, specifically inonError(Throwable)
oronComplete()
, and handled there (for example, by logging it or presenting the user with an error dialog). However, if the error would be thrown fromonNext(Object)
, Rule 2.13 mandates the implementation callsDisposable.dispose()
and signals the exception in a way that is adequate to the target context, for example, by callingonError(Throwable)
on the sameObserver
instance.If, for some reason, the
Observer
won't follow Rule 2.13, theObservable.safeSubscribe(Observer)
can wrap it with the necessary safeguards and route exceptions thrown fromonNext
intoonError
and route exceptions thrown fromonError
andonComplete
into the global error handler viaRxJavaPlugins.onError(Throwable)
.- See Also:
- ReactiveX documentation: Observable
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
onComplete()
Notifies theObserver
that theObservable
has finished sending push-based notifications.void
onError(@NonNull java.lang.Throwable e)
Notifies theObserver
that theObservable
has experienced an error condition.void
onNext(@NonNull T t)
Provides theObserver
with a new item to observe.void
onSubscribe(@NonNull Disposable d)
Provides theObserver
with the means of cancelling (disposing) the connection (channel) with theObservable
in both synchronous (from withinonNext(Object)
) and asynchronous manner.
-
-
-
Method Detail
-
onSubscribe
void onSubscribe(@NonNull @NonNull Disposable d)
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
void onNext(@NonNull @NonNull T t)
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
void onError(@NonNull @NonNull java.lang.Throwable e)
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)
.
-
-