KEMBAR78
Completable (RxJava Javadoc 3.1.12)
io.reactivex.rxjava3.core

Class Completable

  • All Implemented Interfaces:
    CompletableSource
    Direct Known Subclasses:
    CompletableSubject


    public abstract class Completable
    extends Object
    implements CompletableSource
    The Completable class represents a deferred computation without any value but only indication for completion or exception.

    Completable behaves similarly to Observable except that it can only emit either a completion or error signal (there is no onNext or onSuccess as with the other reactive types).

    The Completable class implements the CompletableSource base interface and the default consumer type it interacts with is the CompletableObserver via the subscribe(CompletableObserver) method. The Completable operates with the following sequential protocol:

    
         onSubscribe (onError | onComplete)?
     

    Note that as with the Observable protocol, onError and onComplete are mutually exclusive events.

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

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

    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.

    Example:

    
     Disposable d = Completable.complete()
        .delay(10, TimeUnit.SECONDS, Schedulers.io())
        .subscribeWith(new DisposableCompletableObserver() {
            @Override
            public void onStart() {
                System.out.println("Started");
            }
    
            @Override
            public void onError(Throwable error) {
                error.printStackTrace();
            }
    
            @Override
            public void onComplete() {
                System.out.println("Done!");
            }
        });
     
     Thread.sleep(5000);
     
     d.dispose();
     

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

    See Also:
    DisposableCompletableObserver