T - the value type to emitpublic interface ObservableEmitter<T> extends Emitter<T>
Observer that allows associating
a resource with it.
The Emitter.onNext(Object), Emitter.onError(Throwable), tryOnError(Throwable)
and Emitter.onComplete() methods should be called in a sequential manner, just like the
Observer's methods should be.
Use the ObservableEmitter the serialize() method returns instead of the original
ObservableEmitter instance provided by the generator routine if you want to ensure this.
The other methods are thread-safe.
The emitter allows the registration of a single resource, in the form of a Disposable
or Cancellable via setDisposable(Disposable) or setCancellable(Cancellable)
respectively. The emitter implementations will dispose/cancel this instance when the
downstream cancels the flow or after the event generator logic calls Emitter.onError(Throwable),
Emitter.onComplete() or when tryOnError(Throwable) succeeds.
Only one Disposable or Cancellable object can be associated with the emitter at
a time. Calling either set method will dispose/cancel any previous object. If there
is a need for handling multiple resources, one can create a CompositeDisposable
and associate that with the emitter instead.
The Cancellable is logically equivalent to Disposable but allows using cleanup logic that can
throw a checked exception (such as many close() methods on Java IO components). Since
the release of resources happens after the terminal events have been delivered or the sequence gets
cancelled, exceptions throw within Cancellable are routed to the global error handler via
RxJavaPlugins.onError(Throwable).
| Modifier and Type | Method and Description |
|---|---|
boolean |
isDisposed()
Returns true if the downstream disposed the sequence or the
emitter was terminated via
Emitter.onError(Throwable), Emitter.onComplete() or a
successful tryOnError(Throwable). |
@NonNull ObservableEmitter<T> |
serialize()
Ensures that calls to
onNext, onError and onComplete are properly serialized. |
void |
setCancellable(@Nullable Cancellable c)
Sets a
Cancellable on this emitter; any previous Disposable
or Cancellable will be disposed/cancelled. |
void |
setDisposable(@Nullable Disposable d)
Sets a
Disposable on this emitter; any previous Disposable
or Cancellable will be disposed/cancelled. |
boolean |
tryOnError(@NonNull Throwable t)
Attempts to emit the specified
Throwable error if the downstream
hasn't cancelled the sequence or is otherwise terminated, returning false
if the emission is not allowed to happen due to lifecycle restrictions. |
onComplete, onError, onNextvoid setDisposable(@Nullable @Nullable Disposable d)
Disposable on this emitter; any previous Disposable
or Cancellable will be disposed/cancelled.
This method is thread-safe.
d - the Disposable, null is allowedvoid setCancellable(@Nullable @Nullable Cancellable c)
Cancellable on this emitter; any previous Disposable
or Cancellable will be disposed/cancelled.
This method is thread-safe.
c - the Cancellable resource, null is allowedboolean isDisposed()
Emitter.onError(Throwable), Emitter.onComplete() or a
successful tryOnError(Throwable).
This method is thread-safe.
@NonNull @NonNull ObservableEmitter<T> serialize()
onNext, onError and onComplete are properly serialized.ObservableEmitterboolean tryOnError(@NonNull @NonNull Throwable t)
Throwable error if the downstream
hasn't cancelled the sequence or is otherwise terminated, returning false
if the emission is not allowed to happen due to lifecycle restrictions.
Unlike Emitter.onError(Throwable), the RxjavaPlugins.onError
is not called if the error could not be delivered.
History: 2.1.1 - experimental
t - the Throwable error to signal if possible