Class Single<T>

java.lang.Object
io.reactivex.rxjava3.core.Single<T>
Type Parameters:
T - the type of the item emitted by the Single
All Implemented Interfaces:
SingleSource<T>
Direct Known Subclasses:
CompletableMaterialize, CompletableToSingle, FlowableAllSingle, FlowableAnySingle, FlowableCollectSingle, FlowableCollectWithCollectorSingle, FlowableCountSingle, FlowableElementAtSingle, FlowableLastSingle, FlowableReduceSeedSingle, FlowableReduceWithSingle, FlowableSequenceEqualSingle, FlowableSingleSingle, FlowableToListSingle, MaybeContains, MaybeCount, MaybeEqualSingle, MaybeIsEmptySingle, MaybeMaterialize, MaybeSwitchIfEmptySingle, MaybeToSingle, ObservableAllSingle, ObservableAnySingle, ObservableCollectSingle, ObservableCollectWithCollectorSingle, ObservableCountSingle, ObservableElementAtSingle, ObservableLastSingle, ObservableReduceSeedSingle, ObservableReduceWithSingle, ObservableSequenceEqualSingle, ObservableSingleSingle, ObservableToListSingle, SingleAmb, SingleCache, SingleContains, SingleCreate, SingleDefer, SingleDelay, SingleDelayWithCompletable, SingleDelayWithObservable, SingleDelayWithPublisher, SingleDelayWithSingle, SingleDetach, SingleDoAfterSuccess, SingleDoAfterTerminate, SingleDoFinally, SingleDoOnDispose, SingleDoOnError, SingleDoOnEvent, SingleDoOnLifecycle, SingleDoOnSubscribe, SingleDoOnSuccess, SingleDoOnTerminate, SingleEquals, SingleError, SingleFlatMap, SingleFlatMapBiSelector, SingleFlatMapNotification, SingleFromCallable, SingleFromCompletionStage, SingleFromPublisher, SingleFromSupplier, SingleFromUnsafeSource, SingleHide, SingleJust, SingleLift, SingleMap, SingleMaterialize, SingleNever, SingleObserveOn, SingleOnErrorReturn, SingleResumeNext, SingleSubject, SingleSubscribeOn, SingleTakeUntil, SingleTimeInterval, SingleTimeout, SingleTimer, SingleUnsubscribeOn, SingleUsing, SingleZipArray, SingleZipIterable

public abstract class Single<@NonNull T> extends Object implements SingleSource<T>
The Single class implements the Reactive Pattern for a single value response.

Single behaves similarly to Observable except that it can only emit either a single successful value or an error (there is no onComplete notification as there is for an Observable).

The Single class implements the SingleSource base interface and the default consumer type it interacts with is the SingleObserver via the subscribe(SingleObserver) method.

The Single operates with the following sequential protocol:

     onSubscribe (onSuccess | onError)?
 

Note that onSuccess and onError are mutually exclusive events; unlike Observable, onSuccess is never followed by onError.

Like Observable, a running Single can be stopped through the Disposable instance provided to consumers through SingleObserver.onSubscribe(io.reactivex.rxjava3.disposables.Disposable).

Like an Observable, a Single is lazy, can be either "hot" or "cold", synchronous or asynchronous. Single instances returned by the methods of this class are cold and there is a standard hot implementation in the form of a subject: SingleSubject.

The documentation for this class makes use of marble diagrams. The following legend explains these diagrams:

See Flowable or Observable for the implementation of the Reactive Pattern for a stream or vector of values.

For more information see the ReactiveX documentation.

Example:


 Disposable d = Single.just("Hello World")
    .delay(10, TimeUnit.SECONDS, Schedulers.io())
    .subscribeWith(new DisposableSingleObserver<String>() {
        @Override
        public void onStart() {
            System.out.println("Started");
        }

        @Override
        public void onSuccess(String value) {
            System.out.println("Success: " + value);
        }

        @Override
        public void onError(Throwable error) {
            error.printStackTrace();
        }
    });
 
 Thread.sleep(5000);
 
 d.dispose();
 

Note that by design, subscriptions via subscribe(SingleObserver) can't be disposed from the outside (hence the void return of the subscribe(SingleObserver) method) and it is the responsibility of the implementor of the SingleObserver to allow this to happen. RxJava supports such usage with the standard DisposableSingleObserver instance. For convenience, the subscribeWith(SingleObserver) method is provided as well to allow working with a SingleObserver (or subclass) instance to be applied with in a fluent manner (such as in the example above).

Since:
2.0
See Also: