Class Completable
- All Implemented Interfaces:
CompletableSource
- Direct Known Subclasses:
CompletableSubject
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(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:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic @NonNull Completableamb(@NonNull Iterable<@NonNull ? extends CompletableSource> sources) Returns aCompletablewhich terminates as soon as one of the sourceCompletables in theIterablesequence terminates (normally or with an error) and disposes all otherCompletables.static @NonNull CompletableambArray(@NonNull CompletableSource... sources) Returns aCompletablewhich terminates as soon as one of the sourceCompletables terminates (normally or with an error) and disposes all otherCompletables.final @NonNull CompletableambWith(@NonNull CompletableSource other) Returns aCompletablethat emits the a terminated event of either thisCompletableor the otherCompletableSource, whichever fires first.final @NonNull CompletableandThen(@NonNull CompletableSource next) andThen(@NonNull MaybeSource<@NonNull T> next) Returns aMaybewhich will subscribe to thisCompletableand once that is completed then will subscribe to thenextMaybeSource.final <@NonNull T>
@NonNull Observable<T> andThen(@NonNull ObservableSource<@NonNull T> next) Returns anObservablewhich will subscribe to thisCompletableand once that is completed then will subscribe to thenextObservableSource.andThen(@NonNull SingleSource<@NonNull T> next) Returns aSinglewhich will subscribe to thisCompletableand once that is completed then will subscribe to thenextSingleSource.andThen(@NonNull Flow.Publisher<@NonNull T> next) Returns aFlowablewhich will subscribe to thisCompletableand once that is completed then will subscribe to thenextFlow.Publisher.final voidSubscribes to and awaits the termination of thisCompletableinstance in a blocking manner and rethrows any exception emitted.final booleanblockingAwait(long timeout, @NonNull TimeUnit unit) Subscribes to and awaits the termination of thisCompletableinstance in a blocking manner with a specific timeout and rethrows any exception emitted within the timeout window.final voidSubscribes to the currentCompletableand blocks the current thread until it terminates.final voidblockingSubscribe(@NonNull CompletableObserver observer) Subscribes to the currentCompletableand calls the appropriateCompletableObservermethod on the current thread.final voidblockingSubscribe(@NonNull Action onComplete) Subscribes to the currentCompletableand calls givenonCompletecallback on the current thread when it completes normally.final voidSubscribes to the currentCompletableand calls the appropriate callback on the current thread when it terminates.final @NonNull Completablecache()Subscribes to thisCompletableonly once, when the firstCompletableObserversubscribes to the resultCompletable, caches its terminal event and relays/replays it to observers.static @NonNull Completablecomplete()Returns aCompletableinstance that completes immediately when subscribed to.final @NonNull Completablecompose(@NonNull CompletableTransformer transformer) Calls the given transformer function with this instance and returns the function's resultingCompletableSourcewrapped withwrap(CompletableSource).static @NonNull Completableconcat(@NonNull Iterable<@NonNull ? extends CompletableSource> sources) Returns aCompletablewhich completes only when all sources complete, one after another.static @NonNull Completableconcat(@NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources) Returns aCompletablewhich completes only when all sources complete, one after another.static @NonNull Completableconcat(@NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources, int prefetch) Returns aCompletablewhich completes only when all sources complete, one after another.static @NonNull CompletableconcatArray(@NonNull CompletableSource... sources) Returns aCompletablewhich completes only when all sources complete, one after another.static @NonNull CompletableconcatArrayDelayError(@NonNull CompletableSource... sources) Returns aCompletablewhich completes only when all sources complete, one after another.static @NonNull CompletableconcatDelayError(@NonNull Iterable<@NonNull ? extends CompletableSource> sources) Returns aCompletablewhich completes only when all sources complete, one after another.static @NonNull CompletableconcatDelayError(@NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources) Returns aCompletablewhich completes only when all sources complete, one after another.static @NonNull CompletableconcatDelayError(@NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources, int prefetch) Returns aCompletablewhich completes only when all sources complete, one after another.final @NonNull CompletableconcatWith(@NonNull CompletableSource other) Concatenates thisCompletablewith anotherCompletableSource.static @NonNull Completablecreate(@NonNull CompletableOnSubscribe source) Provides an API (via a coldCompletable) that bridges the reactive world with the callback-style world.static @NonNull Completabledefer(@NonNull Supplier<? extends @NonNull CompletableSource> supplier) Defers the subscription to aCompletableinstance returned by a supplier.final @NonNull CompletableReturns aCompletablewhich delays the emission of the completion event by the given time.final @NonNull CompletableReturns aCompletablewhich delays the emission of the completion event by the given time while running on the specifiedScheduler.final @NonNull CompletableReturns aCompletablewhich delays the emission of the completion event, and optionally the error as well, by the given time while running on the specifiedScheduler.final @NonNull CompletabledelaySubscription(long time, @NonNull TimeUnit unit) Returns aCompletablethat delays the subscription to the upstream by a given amount of time.final @NonNull CompletabledelaySubscription(long time, @NonNull TimeUnit unit, @NonNull Scheduler scheduler) Returns aCompletablethat delays the subscription to the upstream by a given amount of time, both waiting and subscribing on a givenScheduler.final @NonNull CompletabledoAfterTerminate(@NonNull Action onAfterTerminate) Returns aCompletableinstance that calls the givenonAfterTerminateActionafter thisCompletablecompletes normally or with an exception.final @NonNull CompletableCalls the specifiedActionafter thisCompletablesignalsonErrororonCompleteor gets disposed by the downstream.final @NonNull CompletabledoOnComplete(@NonNull Action onComplete) final @NonNull CompletabledoOnDispose(@NonNull Action onDispose) Calls the sharedActionif aCompletableObserversubscribed to the currentCompletabledisposes the commonDisposableit received viaonSubscribe.final @NonNull Completablefinal @NonNull Completablefinal @NonNull CompletabledoOnLifecycle(@NonNull Consumer<? super Disposable> onSubscribe, @NonNull Action onDispose) Calls the appropriateonXXXmethod (shared between allCompletableObservers) for the lifecycle events of the sequence (subscription, disposal).final @NonNull CompletabledoOnSubscribe(@NonNull Consumer<? super Disposable> onSubscribe) Returns aCompletableinstance that calls the givenonSubscribecallback with the disposable that the downstreamCompletableObservers receive upon subscription.final @NonNull CompletabledoOnTerminate(@NonNull Action onTerminate) Returns aCompletableinstance that calls the givenonTerminateActionjust before thisCompletablecompletes normally or with an exception.static @NonNull CompletableCreates aCompletablewhich calls the given error supplier for each subscriber and emits its returnedThrowable.static @NonNull CompletableCreates aCompletableinstance that emits the givenThrowableexception to subscribers.static @NonNull CompletablefromAction(@NonNull Action action) Returns aCompletableinstance that runs the givenActionfor eachCompletableObserverand emits either an exception or simply completes.static @NonNull CompletablefromCallable(@NonNull Callable<?> callable) Returns aCompletablewhich when subscribed, executes theCallablefunction, ignores its normal result and emitsonErrororonCompleteonly.static @NonNull CompletablefromCompletionStage(@NonNull CompletionStage<?> stage) Signals completion (or error) when theCompletionStageterminates.static @NonNull CompletablefromFuture(@NonNull Future<?> future) Returns aCompletableinstance that reacts to the termination of the givenFuturein a blocking fashion.static <@NonNull T>
@NonNull CompletablefromMaybe(@NonNull MaybeSource<@NonNull T> maybe) Returns aCompletableinstance that when subscribed to, subscribes to theMaybeSourceinstance and emits anonCompleteevent if the maybe emitsonSuccess/onCompleteor forwards anyonErrorevents.static <@NonNull T>
@NonNull CompletablefromObservable(@NonNull ObservableSource<@NonNull T> observable) Returns aCompletableinstance that subscribes to the givenObservableSource, ignores all values and emits only the terminal event.static <@NonNull T>
@NonNull CompletablefromPublisher(@NonNull Flow.Publisher<@NonNull T> publisher) Returns aCompletableinstance that subscribes to the givenFlow.Publisher, ignores all values and emits only the terminal event.static @NonNull CompletablefromRunnable(@NonNull Runnable run) Returns aCompletableinstance that runs the givenRunnablefor eachCompletableObserverand emits either its unchecked exception or simply completes.static <@NonNull T>
@NonNull CompletablefromSingle(@NonNull SingleSource<@NonNull T> single) Returns aCompletableinstance that when subscribed to, subscribes to theSingleSourceinstance and emits a completion event if the single emitsonSuccessor forwards anyonErrorevents.static @NonNull CompletablefromSupplier(@NonNull Supplier<?> supplier) Returns aCompletablewhich when subscribed, executes theSupplierfunction, ignores its normal result and emitsonErrororonCompleteonly.final @NonNull Completablehide()Hides the identity of thisCompletableand itsDisposable.final @NonNull Completablelift(@NonNull CompletableOperator onLift) This method requires advanced knowledge about building operators, please consider other standard composition methods first; Returns aCompletablewhich, when subscribed to, invokes theapply(CompletableObserver)method of the providedCompletableOperatorfor each individual downstreamCompletableand allows the insertion of a custom operator by accessing the downstream'sCompletableObserverduring this subscription phase and providing a newCompletableObserver, containing the custom operator's intended business logic, that will be used in the subscription process going further upstream.final <@NonNull T>
@NonNull Single<Notification<T>> Maps the signal types of thisCompletableinto aNotificationof the same kind and emits it as a single success value to downstream.static @NonNull Completablemerge(@NonNull Iterable<@NonNull ? extends CompletableSource> sources) Returns aCompletableinstance that subscribes to all sources at once and completes only when all sourceCompletableSources complete or one of them emits an error.static @NonNull Completablemerge(@NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources) Returns aCompletableinstance that subscribes to all sources at once and completes only when all sourceCompletableSources complete or one of them emits an error.static @NonNull Completablemerge(@NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources, int maxConcurrency) Returns aCompletableinstance that keeps subscriptions to a limited number of sources at once and completes only when all sourceCompletableSources complete or one of them emits an error.static @NonNull CompletablemergeArray(@NonNull CompletableSource... sources) Returns aCompletableinstance that subscribes to all sources at once and completes only when all sourceCompletableSources complete or one of them emits an error.static @NonNull CompletablemergeArrayDelayError(@NonNull CompletableSource... sources) Returns aCompletablethat subscribes to allCompletableSources in the source array and delays any error emitted by any of the innerCompletableSources until all of them terminate in a way or another.static @NonNull CompletablemergeDelayError(@NonNull Iterable<@NonNull ? extends CompletableSource> sources) Returns aCompletablethat subscribes to allCompletableSources in the source sequence and delays any error emitted by any of the innerCompletableSources until all of them terminate in a way or another.static @NonNull CompletablemergeDelayError(@NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources) Returns aCompletablethat subscribes to allCompletableSources in the source sequence and delays any error emitted by either the sourcesFlow.Publisheror any of the innerCompletableSources until all of them terminate in a way or another.static @NonNull CompletablemergeDelayError(@NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources, int maxConcurrency) Returns aCompletablethat subscribes to a limited number of innerCompletableSources at once in the source sequence and delays any error emitted by either the sourcesFlow.Publisheror any of the innerCompletableSources until all of them terminate in a way or another.final @NonNull CompletablemergeWith(@NonNull CompletableSource other) Returns aCompletablewhich subscribes to this and the otherCompletableSourceand completes when both of them complete or one emits an error.static @NonNull Completablenever()Returns aCompletablethat never callsonErrororonComplete.final @NonNull CompletableReturns aCompletablewhich emits the terminal events from the thread of the specifiedScheduler.final @NonNull CompletableReturns aCompletableinstance that if thisCompletableemits an error, it will emit anonCompleteand swallow the upstreamThrowable.final @NonNull CompletableonErrorComplete(@NonNull Predicate<? super Throwable> predicate) final @NonNull CompletableonErrorResumeNext(@NonNull Function<? super Throwable, ? extends CompletableSource> fallbackSupplier) Returns aCompletableinstance that when encounters an error from thisCompletable, calls the specifiedmapperFunctionthat returns aCompletableSourceinstance for it and resumes the execution with it.final @NonNull CompletableonErrorResumeWith(@NonNull CompletableSource fallback) Resumes the flow with the givenCompletableSourcewhen the currentCompletablefails instead of signaling the error viaonError.onErrorReturn(@NonNull Function<? super Throwable, ? extends @NonNull T> itemSupplier) Ends the flow with a success item returned by a function for theThrowableerror signaled by the currentCompletableinstead of signaling the error viaonError.onErrorReturnItem(@NonNull T item) Ends the flow with the given success item when the currentCompletablefails instead of signaling the error viaonError.final @NonNull CompletableNulls out references to the upstream producer and downstreamCompletableObserverif the sequence is terminated or downstream callsdispose().final @NonNull Completablerepeat()Returns aCompletablethat repeatedly subscribes to thisCompletableuntil disposed.final @NonNull Completablerepeat(long times) Returns aCompletablethat subscribes repeatedly at most the given number of times to thisCompletable.final @NonNull CompletableReturns aCompletablethat repeatedly subscribes to thisCompletableso long as the given stopBooleanSupplierreturnsfalse.final @NonNull CompletablerepeatWhen(@NonNull Function<? super Flowable<Object>, @NonNull ? extends Flow.Publisher<@NonNull ?>> handler) Returns aCompletableinstance that repeats when theFlow.Publisherreturned by the handlerFunctionemits an item or completes when thisPublisheremits anonCompleteevent.final @NonNull Completableretry()Returns aCompletablethat retries thisCompletableas long as it emits anonErrorevent.final @NonNull Completableretry(long times) Returns aCompletablethat when thisCompletableemits an error, retries at most the given number of times before giving up and emitting the last error.final @NonNull CompletableReturns aCompletablethat when thisCompletableemits an error, retries at most times or until the predicate returnsfalse, whichever happens first and emitting the last error.final @NonNull Completableretry(@NonNull BiPredicate<? super Integer, ? super Throwable> predicate) Returns aCompletablethat retries thisCompletablein case of an error as long as thepredicatereturnstrue.final @NonNull CompletableReturns aCompletablethat when thisCompletableemits an error, calls the given predicate with the latestThrowableto decide whether to resubscribe to the upstream or not.final @NonNull CompletableRetries until the given stop function returnstrue.final @NonNull CompletableretryWhen(@NonNull Function<? super Flowable<Throwable>, @NonNull ? extends Flow.Publisher<@NonNull ?>> handler) Returns aCompletablewhich given aFlow.Publisherand when thisCompletableemits an error, delivers that error through aFlowableand thePublishershould signal a value indicating a retry in response or a terminal event indicating a termination.final voidsafeSubscribe(@NonNull CompletableObserver observer) Wraps the givenCompletableObserver, catches anyRuntimeExceptions thrown by itsCompletableObserver.onSubscribe(Disposable),CompletableObserver.onError(Throwable)orCompletableObserver.onComplete()methods and routes those to the global error handler viaRxJavaPlugins.onError(Throwable).sequenceEqual(@NonNull CompletableSource source1, @NonNull CompletableSource source2) final @NonNull CompletablestartWith(@NonNull CompletableSource other) Returns aCompletablewhich first runs the otherCompletableSourcethen the currentCompletableif the other completed normally.startWith(@NonNull MaybeSource<@NonNull T> other) Returns aFlowablewhich first runs the otherMaybeSourcethen the currentCompletableif the other succeeded or completed normally.final <@NonNull T>
@NonNull Observable<T> startWith(@NonNull ObservableSource<@NonNull T> other) Returns anObservablewhich first delivers the events of the otherObservableSourcethen runs the currentCompletable.startWith(@NonNull SingleSource<@NonNull T> other) Returns aFlowablewhich first runs the otherSingleSourcethen the currentCompletableif the other succeeded normally.startWith(@NonNull Flow.Publisher<@NonNull T> other) Returns aFlowablewhich first delivers the events of the otherFlow.Publisherthen runs the currentCompletable.final @NonNull DisposableSubscribes to thisCompletableand returns aDisposablewhich can be used to dispose the subscription.final voidsubscribe(@NonNull CompletableObserver observer) Subscribes the givenCompletableObserverto thisCompletableSourceinstance.final @NonNull Disposablefinal @NonNull DisposableSubscribes to thisCompletableand calls back either theonErrororonCompletefunctions.final @NonNull Disposablesubscribe(@NonNull Action onComplete, @NonNull Consumer<? super Throwable> onError, @NonNull DisposableContainer container) Wraps the given onXXX callbacks into aDisposableCompletableObserver, adds it to the givenDisposableContainerand ensures, that if the upstream terminates or this particularDisposableis disposed, theCompletableObserveris removed from the given composite.protected abstract voidsubscribeActual(@NonNull CompletableObserver observer) Implement this method to handle the incomingCompletableObservers and perform the business logic in your operator.final @NonNull CompletablesubscribeOn(@NonNull Scheduler scheduler) Returns aCompletablewhich subscribes the downstream subscriber on the specified scheduler, making sure the subscription side-effects happen on that specific thread of theScheduler.final <@NonNull E extends CompletableObserver>
EsubscribeWith(@NonNull E observer) Subscribes a givenCompletableObserver(subclass) to thisCompletableand returns the givenCompletableObserveras is.static @NonNull CompletableswitchOnNext(@NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources) Switches betweenCompletableSources emitted by the sourceFlow.Publisherwhenever a newCompletableSourceis emitted, disposing the previously runningCompletableSource, exposing the setup as aCompletablesequence.static @NonNull CompletableswitchOnNextDelayError(@NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources) Switches betweenCompletableSources emitted by the sourceFlow.Publisherwhenever a newCompletableSourceis emitted, disposing the previously runningCompletableSource, exposing the setup as aCompletablesequence and delaying all errors from all of them until all terminate.final @NonNull CompletabletakeUntil(@NonNull CompletableSource other) Terminates the downstream if this or the otherCompletableterminates (wins the termination race) while disposing the connection to the losing source.final @NonNull TestObserver<Void> test()Creates aTestObserverand subscribes it to thisCompletable.final @NonNull TestObserver<Void> test(boolean dispose) Creates aTestObserveroptionally in cancelled state, then subscribes it to thisCompletable.final @NonNull CompletableReturns aCompletable that runs thisCompletableand emits aTimeoutExceptionin case thisCompletabledoesn't complete within the given time.final @NonNull Completabletimeout(long timeout, @NonNull TimeUnit unit, @NonNull CompletableSource fallback) Returns aCompletablethat runs thisCompletableand switches to the otherCompletableSourcein case thisCompletabledoesn't complete within the given time.final @NonNull CompletableReturns aCompletablethat runs thisCompletableand emits aTimeoutExceptionin case thisCompletabledoesn't complete within the given time while "waiting" on the specifiedScheduler.final @NonNull Completabletimeout(long timeout, @NonNull TimeUnit unit, @NonNull Scheduler scheduler, @NonNull CompletableSource fallback) Returns aCompletablethat runs thisCompletableand switches to the otherCompletableSourcein case thisCompletabledoesn't complete within the given time while "waiting" on the specifiedScheduler.static @NonNull CompletableReturns aCompletableinstance that fires itsonCompleteevent after the given delay elapsed.static @NonNull CompletableReturns aCompletableinstance that fires itsonCompleteevent after the given delay elapsed by using the suppliedScheduler.final <R> Rto(@NonNull CompletableConverter<? extends R> converter) Calls the specifiedCompletableConverterfunction during assembly time and returns its resulting value.final <@Nullable T>
@NonNull CompletionStage<T> toCompletionStage(@Nullable T defaultItem) Signals the given default item when the upstream completes or signals the upstream error via aCompletionStage.Returns aFlowablewhich when subscribed to subscribes to thisCompletableand relays the terminal events to the downstreamFlow.Subscriber.toFuture()toMaybe()Converts thisCompletableinto aMaybe.final <@NonNull T>
@NonNull Observable<T> Returns anObservablewhich when subscribed to subscribes to thisCompletableand relays the terminal events to the downstreamObserver.toSingleDefault(@NonNull T completionValue) Converts thisCompletableinto aSinglewhich when thisCompletablecompletes normally, emits the given value throughonSuccess.static @NonNull CompletableunsafeCreate(@NonNull CompletableSource onSubscribe) Constructs aCompletableinstance by wrapping the given source callback without any safeguards; you should manage the lifecycle and response to downstream disposal.final @NonNull CompletableunsubscribeOn(@NonNull Scheduler scheduler) Returns aCompletablewhich makes sure when an observer disposes the subscription, thedispose()method is called on the specifiedScheduler.static <@NonNull R>
@NonNull Completableusing(@NonNull Supplier<@NonNull R> resourceSupplier, @NonNull Function<? super @NonNull R, ? extends CompletableSource> sourceSupplier, @NonNull Consumer<? super @NonNull R> resourceCleanup) Returns aCompletableinstance which manages a resource along with a customCompletableSourceinstance while the subscription is active.static <@NonNull R>
@NonNull Completableusing(@NonNull Supplier<@NonNull R> resourceSupplier, @NonNull Function<? super @NonNull R, ? extends CompletableSource> sourceSupplier, @NonNull Consumer<? super @NonNull R> resourceCleanup, boolean eager) Returns aCompletableinstance which manages a resource along with a customCompletableSourceinstance while the subscription is active and performs eager or lazy resource disposition.static @NonNull Completablewrap(@NonNull CompletableSource source)
-
Constructor Details
-
Completable
public Completable()
-
-
Method Details
-
ambArray
@CheckReturnValue @NonNull @SchedulerSupport("none") @SafeVarargs public static @NonNull Completable ambArray(@NonNull @NonNull CompletableSource... sources) Returns aCompletablewhich terminates as soon as one of the sourceCompletables terminates (normally or with an error) and disposes all otherCompletables.
- Scheduler:
ambArraydoes not operate by default on a particularScheduler.
- Parameters:
sources- the array of sourceCompletables. A subscription to each source will occur in the same order as in this array.- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnull
-
amb
@CheckReturnValue @NonNull @SchedulerSupport("none") public static @NonNull Completable amb(@NonNull @NonNull Iterable<@NonNull ? extends CompletableSource> sources) Returns aCompletablewhich terminates as soon as one of the sourceCompletables in theIterablesequence terminates (normally or with an error) and disposes all otherCompletables.
- Scheduler:
ambdoes not operate by default on a particularScheduler.
- Parameters:
sources- theIterableof sourceCompletables. A subscription to each source will occur in the same order as in thisIterable.- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnull
-
complete
Returns aCompletableinstance that completes immediately when subscribed to.
- Scheduler:
completedoes not operate by default on a particularScheduler.
- Returns:
- the shared
Completableinstance
-
concatArray
@CheckReturnValue @NonNull @SchedulerSupport("none") @SafeVarargs public static @NonNull Completable concatArray(@NonNull @NonNull CompletableSource... sources) Returns aCompletablewhich completes only when all sources complete, one after another.
- Scheduler:
concatArraydoes not operate by default on a particularScheduler.
- Parameters:
sources- the sources to concatenate- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnull
-
concatArrayDelayError
@CheckReturnValue @NonNull @SchedulerSupport("none") @SafeVarargs public static @NonNull Completable concatArrayDelayError(@NonNull @NonNull CompletableSource... sources) Returns aCompletablewhich completes only when all sources complete, one after another.
- Scheduler:
concatArrayDelayErrordoes not operate by default on a particularScheduler.
- Parameters:
sources- the sources to concatenate- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnull- Since:
- 3.0.0
-
concat
@CheckReturnValue @NonNull @SchedulerSupport("none") public static @NonNull Completable concat(@NonNull @NonNull Iterable<@NonNull ? extends CompletableSource> sources) Returns aCompletablewhich completes only when all sources complete, one after another.
- Scheduler:
concatdoes not operate by default on a particularScheduler.
- Parameters:
sources- the sources to concatenate- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnull
-
concat
@CheckReturnValue @SchedulerSupport("none") @BackpressureSupport(FULL) @NonNull public static @NonNull Completable concat(@NonNull @NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources) Returns aCompletablewhich completes only when all sources complete, one after another.
- Backpressure:
- The returned
Completablehonors the backpressure of the downstream consumer and expects the otherFlow.Publisherto honor it as well. - Scheduler:
concatdoes not operate by default on a particularScheduler.
- Parameters:
sources- the sources to concatenate- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnull
-
concat
@CheckReturnValue @NonNull @SchedulerSupport("none") @BackpressureSupport(FULL) public static @NonNull Completable concat(@NonNull @NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources, int prefetch) Returns aCompletablewhich completes only when all sources complete, one after another.
- Backpressure:
- The returned
Completablehonors the backpressure of the downstream consumer and expects the otherFlow.Publisherto honor it as well. - Scheduler:
concatdoes not operate by default on a particularScheduler.
- Parameters:
sources- the sources to concatenateprefetch- the number of sources to prefetch from the sources- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnullIllegalArgumentException- ifprefetchis non-positive
-
concatDelayError
@CheckReturnValue @NonNull @SchedulerSupport("none") public static @NonNull Completable concatDelayError(@NonNull @NonNull Iterable<@NonNull ? extends CompletableSource> sources) Returns aCompletablewhich completes only when all sources complete, one after another.
- Scheduler:
concatDelayErrordoes not operate by default on a particularScheduler.
- Parameters:
sources- the sources to concatenate- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnull- Since:
- 3.0.0
-
concatDelayError
@CheckReturnValue @SchedulerSupport("none") @BackpressureSupport(FULL) @NonNull public static @NonNull Completable concatDelayError(@NonNull @NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources) Returns aCompletablewhich completes only when all sources complete, one after another.
- Backpressure:
- The returned
Completablehonors the backpressure of the downstream consumer and expects the otherFlow.Publisherto honor it as well. - Scheduler:
concatDelayErrordoes not operate by default on a particularScheduler.
- Parameters:
sources- the sources to concatenate- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnull- Since:
- 3.0.0
-
concatDelayError
@CheckReturnValue @NonNull @SchedulerSupport("none") @BackpressureSupport(FULL) public static @NonNull Completable concatDelayError(@NonNull @NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources, int prefetch) Returns aCompletablewhich completes only when all sources complete, one after another.
- Backpressure:
- The returned
Completablehonors the backpressure of the downstream consumer and expects the otherFlow.Publisherto honor it as well. - Scheduler:
concatDelayErrordoes not operate by default on a particularScheduler.
- Parameters:
sources- the sources to concatenateprefetch- the number of sources to prefetch from the sources- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnullIllegalArgumentException- ifprefetchis non-positive- Since:
- 3.0.0
-
create
@CheckReturnValue @NonNull @SchedulerSupport("none") public static @NonNull Completable create(@NonNull @NonNull CompletableOnSubscribe source) Provides an API (via a coldCompletable) that bridges the reactive world with the callback-style world.
Example:
Completable.create(emitter -> { Callback listener = new Callback() { @Override public void onEvent(Event e) { emitter.onComplete(); } @Override public void onFailure(Exception e) { emitter.onError(e); } }; AutoCloseable c = api.someMethod(listener); emitter.setCancellable(c::close); });Whenever a
CompletableObserversubscribes to the returnedCompletable, the providedCompletableOnSubscribecallback is invoked with a fresh instance of aCompletableEmitterthat will interact only with that specificCompletableObserver. If thisCompletableObserverdisposes the flow (makingCompletableEmitter.isDisposed()returntrue), other observers subscribed to the same returnedCompletableare not affected.- Scheduler:
createdoes not operate by default on a particularScheduler.
- Parameters:
source- the emitter that is called when aCompletableObserversubscribes to the returnedCompletable- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourceisnull- See Also:
-
sequenceEqual
@CheckReturnValue @NonNull @SchedulerSupport("none") public static @NonNull Single<Boolean> sequenceEqual(@NonNull @NonNull CompletableSource source1, @NonNull @NonNull CompletableSource source2) Compares twoCompletableSources and emitstruevia aSingleif both complete.
- Scheduler:
sequenceEqualdoes not operate by default on a particularScheduler.
- Parameters:
source1- the firstCompletableSourceinstancesource2- the secondCompletableSourceinstance- Returns:
- the new
Singleinstance - Throws:
NullPointerException- ifsource1orsource2isnull- Since:
- 3.0.0
-
unsafeCreate
@CheckReturnValue @NonNull @SchedulerSupport("none") public static @NonNull Completable unsafeCreate(@NonNull @NonNull CompletableSource onSubscribe) Constructs aCompletableinstance by wrapping the given source callback without any safeguards; you should manage the lifecycle and response to downstream disposal.
- Scheduler:
unsafeCreatedoes not operate by default on a particularScheduler.
- Parameters:
onSubscribe- the callback which will receive theCompletableObserverinstances when theCompletableis subscribed to.- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifonSubscribeisnullIllegalArgumentException- ifsourceis aCompletable
-
defer
@CheckReturnValue @NonNull @SchedulerSupport("none") public static @NonNull Completable defer(@NonNull @NonNull Supplier<? extends @NonNull CompletableSource> supplier) Defers the subscription to aCompletableinstance returned by a supplier.
- Scheduler:
deferdoes not operate by default on a particularScheduler.
- Parameters:
supplier- the supplier that returns theCompletablethat will be subscribed to.- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsupplierisnull
-
error
@CheckReturnValue @NonNull @SchedulerSupport("none") public static @NonNull Completable error(@NonNull @NonNull Supplier<? extends @NonNull Throwable> supplier) Creates aCompletablewhich calls the given error supplier for each subscriber and emits its returnedThrowable.
If the
errorSupplierreturnsnull, the downstreamCompletableObservers will receive aNullPointerException.- Scheduler:
errordoes not operate by default on a particularScheduler.
- Parameters:
supplier- the error supplier, notnull- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsupplierisnull
-
error
@CheckReturnValue @NonNull @SchedulerSupport("none") public static @NonNull Completable error(@NonNull @NonNull Throwable throwable) Creates aCompletableinstance that emits the givenThrowableexception to subscribers.
- Scheduler:
errordoes not operate by default on a particularScheduler.
- Parameters:
throwable- theThrowableinstance to emit, notnull- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifthrowableisnull
-
fromAction
@CheckReturnValue @NonNull @SchedulerSupport("none") public static @NonNull Completable fromAction(@NonNull @NonNull Action action) Returns aCompletableinstance that runs the givenActionfor eachCompletableObserverand emits either an exception or simply completes.
- Scheduler:
fromActiondoes not operate by default on a particularScheduler.- Error handling:
- If the
Actionthrows an exception, the respectiveThrowableis delivered to the downstream viaCompletableObserver.onError(Throwable), except when the downstream has disposed thisCompletablesource. In this latter case, theThrowableis delivered to the global error handler viaRxJavaPlugins.onError(Throwable)as anUndeliverableException.
- Parameters:
action- theActionto run for each subscribingCompletableObserver- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifactionisnull
-
fromCallable
@CheckReturnValue @NonNull @SchedulerSupport("none") public static @NonNull Completable fromCallable(@NonNull @NonNull Callable<?> callable) Returns aCompletablewhich when subscribed, executes theCallablefunction, ignores its normal result and emitsonErrororonCompleteonly.
- Scheduler:
fromCallabledoes not operate by default on a particularScheduler.- Error handling:
- If the
Callablethrows an exception, the respectiveThrowableis delivered to the downstream viaCompletableObserver.onError(Throwable), except when the downstream has disposed thisCompletablesource. In this latter case, theThrowableis delivered to the global error handler viaRxJavaPlugins.onError(Throwable)as anUndeliverableException.
- Parameters:
callable- theCallableinstance to execute for each subscribingCompletableObserver- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifcallableisnull- See Also:
-
fromFuture
@CheckReturnValue @NonNull @SchedulerSupport("none") public static @NonNull Completable fromFuture(@NonNull @NonNull Future<?> future) Returns aCompletableinstance that reacts to the termination of the givenFuturein a blocking fashion.
Note that disposing the
Completablewon't cancel theFuture. UsedoOnDispose(Action)and callFuture.cancel(boolean)in theAction.- Scheduler:
fromFuturedoes not operate by default on a particularScheduler.
- Parameters:
future- theFutureto react to- Returns:
- the new
Completableinstance - Throws:
NullPointerException- iffutureisnull- See Also:
-
fromMaybe
@CheckReturnValue @NonNull @SchedulerSupport("none") public static <@NonNull T> @NonNull Completable fromMaybe(@NonNull @NonNull MaybeSource<@NonNull T> maybe) Returns aCompletableinstance that when subscribed to, subscribes to theMaybeSourceinstance and emits anonCompleteevent if the maybe emitsonSuccess/onCompleteor forwards anyonErrorevents.
- Scheduler:
fromMaybedoes not operate by default on a particularScheduler.
History: 2.1.17 - beta
- Type Parameters:
T- the value type of theMaybeSourceelement- Parameters:
maybe- theMaybeSourceinstance to subscribe to, notnull- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifmaybeisnull- Since:
- 2.2
-
fromRunnable
@CheckReturnValue @NonNull @SchedulerSupport("none") public static @NonNull Completable fromRunnable(@NonNull @NonNull Runnable run) Returns aCompletableinstance that runs the givenRunnablefor eachCompletableObserverand emits either its unchecked exception or simply completes.
If the code to be wrapped needs to throw a checked or more broader
Throwableexception, that exception has to be converted to an unchecked exception by the wrapped code itself. Alternatively, use thefromAction(Action)method which allows the wrapped code to throw anyThrowableexception and will signal it to observers as-is.- Scheduler:
fromRunnabledoes not operate by default on a particularScheduler.- Error handling:
- If the
Runnablethrows an exception, the respectiveThrowableis delivered to the downstream viaCompletableObserver.onError(Throwable), except when the downstream has disposed thisCompletablesource. In this latter case, theThrowableis delivered to the global error handler viaRxJavaPlugins.onError(Throwable)as anUndeliverableException.
- Parameters:
run- theRunnableto run for eachCompletableObserver- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifrunisnull- See Also:
-
fromObservable
@CheckReturnValue @NonNull @SchedulerSupport("none") public static <@NonNull T> @NonNull Completable fromObservable(@NonNull @NonNull ObservableSource<@NonNull T> observable) Returns aCompletableinstance that subscribes to the givenObservableSource, ignores all values and emits only the terminal event.
- Scheduler:
fromObservabledoes not operate by default on a particularScheduler.
- Type Parameters:
T- the type of theObservableSource- Parameters:
observable- theObservableSourceinstance to subscribe to, notnull- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifobservableisnull
-
fromPublisher
@CheckReturnValue @NonNull @BackpressureSupport(UNBOUNDED_IN) @SchedulerSupport("none") public static <@NonNull T> @NonNull Completable fromPublisher(@NonNull @NonNull Flow.Publisher<@NonNull T> publisher) Returns aCompletableinstance that subscribes to the givenFlow.Publisher, ignores all values and emits only the terminal event.
The
Publishermust follow the Reactive-Streams specification. Violating the specification may result in undefined behavior.If possible, use
create(CompletableOnSubscribe)to create a source-likeCompletableinstead.Note that even though
Publisherappears to be a functional interface, it is not recommended to implement it through a lambda as the specification requires state management that is not achievable with a stateless lambda.- Backpressure:
- The returned
Completablehonors the backpressure of the downstream consumer and expects the otherPublisherto honor it as well. - Scheduler:
fromPublisherdoes not operate by default on a particularScheduler.
- Type Parameters:
T- the type of thePublisher- Parameters:
publisher- thePublisherinstance to subscribe to, notnull- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifpublisherisnull- See Also:
-
fromSingle
@CheckReturnValue @NonNull @SchedulerSupport("none") public static <@NonNull T> @NonNull Completable fromSingle(@NonNull @NonNull SingleSource<@NonNull T> single) Returns aCompletableinstance that when subscribed to, subscribes to theSingleSourceinstance and emits a completion event if the single emitsonSuccessor forwards anyonErrorevents.
- Scheduler:
fromSingledoes not operate by default on a particularScheduler.
- Type Parameters:
T- the value type of theSingleSource- Parameters:
single- theSingleSourceinstance to subscribe to, notnull- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsingleisnull
-
fromSupplier
@CheckReturnValue @NonNull @SchedulerSupport("none") public static @NonNull Completable fromSupplier(@NonNull @NonNull Supplier<?> supplier) Returns aCompletablewhich when subscribed, executes theSupplierfunction, ignores its normal result and emitsonErrororonCompleteonly.
- Scheduler:
fromSupplierdoes not operate by default on a particularScheduler.- Error handling:
- If the
Supplierthrows an exception, the respectiveThrowableis delivered to the downstream viaCompletableObserver.onError(Throwable), except when the downstream has disposed thisCompletablesource. In this latter case, theThrowableis delivered to the global error handler viaRxJavaPlugins.onError(Throwable)as anUndeliverableException.
- Parameters:
supplier- theSupplierinstance to execute for eachCompletableObserver- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsupplierisnull- Since:
- 3.0.0
- See Also:
-
mergeArray
@CheckReturnValue @NonNull @SchedulerSupport("none") @SafeVarargs public static @NonNull Completable mergeArray(@NonNull @NonNull CompletableSource... sources) Returns aCompletableinstance that subscribes to all sources at once and completes only when all sourceCompletableSources complete or one of them emits an error.
- Scheduler:
mergeArraydoes not operate by default on a particularScheduler.- Error handling:
- If any of the source
CompletableSources signal aThrowableviaonError, the resultingCompletableterminates with thatThrowableand all other sourceCompletableSources are disposed. If more than oneCompletableSourcesignals an error, the resultingCompletablemay terminate with the first one's error or, depending on the concurrency of the sources, may terminate with aCompositeExceptioncontaining two or more of the various error signals.Throwables that didn't make into the composite will be sent (individually) to the global error handler viaRxJavaPlugins.onError(Throwable)method asUndeliverableExceptionerrors. Similarly,Throwables signaled by source(s) after the returnedCompletablehas been disposed or terminated with a (composite) error will be sent to the same global error handler. UsemergeArrayDelayError(CompletableSource...)to merge sources and terminate only when all sourceCompletableSources have completed or failed with an error.
- Parameters:
sources- the array ofCompletableSources.- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnull- See Also:
-
merge
@CheckReturnValue @NonNull @SchedulerSupport("none") public static @NonNull Completable merge(@NonNull @NonNull Iterable<@NonNull ? extends CompletableSource> sources) Returns aCompletableinstance that subscribes to all sources at once and completes only when all sourceCompletableSources complete or one of them emits an error.
- Scheduler:
mergedoes not operate by default on a particularScheduler.- Error handling:
- If any of the source
CompletableSources signal aThrowableviaonError, the resultingCompletableterminates with thatThrowableand all other sourceCompletableSources are disposed. If more than oneCompletableSourcesignals an error, the resultingCompletablemay terminate with the first one's error or, depending on the concurrency of the sources, may terminate with aCompositeExceptioncontaining two or more of the various error signals.Throwables that didn't make into the composite will be sent (individually) to the global error handler viaRxJavaPlugins.onError(Throwable)method asUndeliverableExceptionerrors. Similarly,Throwables signaled by source(s) after the returnedCompletablehas been disposed or terminated with a (composite) error will be sent to the same global error handler. UsemergeDelayError(Iterable)to merge sources and terminate only when all sourceCompletableSources have completed or failed with an error.
- Parameters:
sources- theIterablesequence ofCompletableSources.- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnull- See Also:
-
merge
@CheckReturnValue @SchedulerSupport("none") @BackpressureSupport(UNBOUNDED_IN) @NonNull public static @NonNull Completable merge(@NonNull @NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources) Returns aCompletableinstance that subscribes to all sources at once and completes only when all sourceCompletableSources complete or one of them emits an error.
- Backpressure:
- The operator consumes the given
Flow.Publisherin an unbounded manner (requestingLong.MAX_VALUEupfront). - Scheduler:
mergedoes not operate by default on a particularScheduler.- Error handling:
- If any of the source
CompletableSources signal aThrowableviaonError, the resultingCompletableterminates with thatThrowableand all other sourceCompletableSources are disposed. If more than oneCompletableSourcesignals an error, the resultingCompletablemay terminate with the first one's error or, depending on the concurrency of the sources, may terminate with aCompositeExceptioncontaining two or more of the various error signals.Throwables that didn't make into the composite will be sent (individually) to the global error handler viaRxJavaPlugins.onError(Throwable)method asUndeliverableExceptionerrors. Similarly,Throwables signaled by source(s) after the returnedCompletablehas been disposed or terminated with a (composite) error will be sent to the same global error handler. UsemergeDelayError(Publisher)to merge sources and terminate only when all sourceCompletableSources have completed or failed with an error.
- Parameters:
sources- thePublishersequence ofCompletableSources.- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnull- See Also:
-
merge
@CheckReturnValue @SchedulerSupport("none") @BackpressureSupport(FULL) @NonNull public static @NonNull Completable merge(@NonNull @NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources, int maxConcurrency) Returns aCompletableinstance that keeps subscriptions to a limited number of sources at once and completes only when all sourceCompletableSources complete or one of them emits an error.
- Backpressure:
- The operator consumes the given
Flow.Publisherin a bounded manner, requestingmaxConcurrencyitems first, then keeps requesting as many more as the innerCompletableSources terminate. - Scheduler:
mergedoes not operate by default on a particularScheduler.- Error handling:
- If any of the source
CompletableSources signal aThrowableviaonError, the resultingCompletableterminates with thatThrowableand all other sourceCompletableSources are disposed. If more than oneCompletableSourcesignals an error, the resultingCompletablemay terminate with the first one's error or, depending on the concurrency of the sources, may terminate with aCompositeExceptioncontaining two or more of the various error signals.Throwables that didn't make into the composite will be sent (individually) to the global error handler viaRxJavaPlugins.onError(Throwable)method asUndeliverableExceptionerrors. Similarly,Throwables signaled by source(s) after the returnedCompletablehas been disposed or terminated with a (composite) error will be sent to the same global error handler. UsemergeDelayError(Publisher, int)to merge sources and terminate only when all sourceCompletableSources have completed or failed with an error.
- Parameters:
sources- thePublishersequence ofCompletableSources.maxConcurrency- the maximum number of concurrent subscriptions- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnullIllegalArgumentException- ifmaxConcurrencyis less than 1- See Also:
-
mergeArrayDelayError
@CheckReturnValue @NonNull @SchedulerSupport("none") @SafeVarargs public static @NonNull Completable mergeArrayDelayError(@NonNull @NonNull CompletableSource... sources) Returns aCompletablethat subscribes to allCompletableSources in the source array and delays any error emitted by any of the innerCompletableSources until all of them terminate in a way or another.
- Scheduler:
mergeArrayDelayErrordoes not operate by default on a particularScheduler.
- Parameters:
sources- the array ofCompletableSources- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnull
-
mergeDelayError
@CheckReturnValue @NonNull @SchedulerSupport("none") public static @NonNull Completable mergeDelayError(@NonNull @NonNull Iterable<@NonNull ? extends CompletableSource> sources) Returns aCompletablethat subscribes to allCompletableSources in the source sequence and delays any error emitted by any of the innerCompletableSources until all of them terminate in a way or another.
- Scheduler:
mergeDelayErrordoes not operate by default on a particularScheduler.
- Parameters:
sources- the sequence ofCompletableSources- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnull
-
mergeDelayError
@CheckReturnValue @SchedulerSupport("none") @BackpressureSupport(UNBOUNDED_IN) @NonNull public static @NonNull Completable mergeDelayError(@NonNull @NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources) Returns aCompletablethat subscribes to allCompletableSources in the source sequence and delays any error emitted by either the sourcesFlow.Publisheror any of the innerCompletableSources until all of them terminate in a way or another.
- Backpressure:
- The operator consumes the
Publisherin an unbounded manner (requestingLong.MAX_VALUEfrom it). - Scheduler:
mergeDelayErrordoes not operate by default on a particularScheduler.
- Parameters:
sources- the sequence ofCompletableSources- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnull
-
mergeDelayError
@CheckReturnValue @SchedulerSupport("none") @BackpressureSupport(FULL) @NonNull public static @NonNull Completable mergeDelayError(@NonNull @NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources, int maxConcurrency) Returns aCompletablethat subscribes to a limited number of innerCompletableSources at once in the source sequence and delays any error emitted by either the sourcesFlow.Publisheror any of the innerCompletableSources until all of them terminate in a way or another.
- Backpressure:
- The operator requests
maxConcurrencyitems from thePublisherupfront and keeps requesting as many more as many innerCompletableSources terminate. - Scheduler:
mergeDelayErrordoes not operate by default on a particularScheduler.
- Parameters:
sources- the sequence ofCompletableSourcesmaxConcurrency- the maximum number of concurrent subscriptions to have at a time to the innerCompletableSources- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnullIllegalArgumentException- ifmaxConcurrencyis non-positive
-
never
Returns aCompletablethat never callsonErrororonComplete.
- Scheduler:
neverdoes not operate by default on a particularScheduler.
- Returns:
- the singleton instance that never calls
onErrororonComplete
-
timer
@CheckReturnValue @SchedulerSupport("io.reactivex:computation") @NonNull public static @NonNull Completable timer(long delay, @NonNull @NonNull TimeUnit unit) Returns aCompletableinstance that fires itsonCompleteevent after the given delay elapsed.
- Scheduler:
timerdoes operate by default on thecomputationScheduler.
- Parameters:
delay- the delay timeunit- the delay unit- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifunitisnull
-
timer
@CheckReturnValue @NonNull @SchedulerSupport("custom") public static @NonNull Completable timer(long delay, @NonNull @NonNull TimeUnit unit, @NonNull @NonNull Scheduler scheduler) Returns aCompletableinstance that fires itsonCompleteevent after the given delay elapsed by using the suppliedScheduler.
- Scheduler:
timeroperates on theScheduleryou specify.
- Parameters:
delay- the delay timeunit- the delay unitscheduler- theSchedulerwhere to emit theonCompleteevent- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifunitorschedulerisnull
-
switchOnNext
@CheckReturnValue @NonNull @SchedulerSupport("none") @BackpressureSupport(UNBOUNDED_IN) public static @NonNull Completable switchOnNext(@NonNull @NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources) Switches betweenCompletableSources emitted by the sourceFlow.Publisherwhenever a newCompletableSourceis emitted, disposing the previously runningCompletableSource, exposing the setup as aCompletablesequence.
- Backpressure:
- The
sourcesPublisheris consumed in an unbounded manner (requestingLong.MAX_VALUE). - Scheduler:
switchOnNextdoes not operate by default on a particularScheduler.- Error handling:
- The returned sequence fails with the first error signaled by the
sourcesPublisheror the currently runningCompletableSource, disposing the rest. Late errors are forwarded to the global error handler viaRxJavaPlugins.onError(Throwable).
- Parameters:
sources- thePublishersequence of innerCompletableSources to switch between- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnull- Since:
- 3.0.0
- See Also:
-
switchOnNextDelayError
@CheckReturnValue @NonNull @SchedulerSupport("none") @BackpressureSupport(UNBOUNDED_IN) public static @NonNull Completable switchOnNextDelayError(@NonNull @NonNull Flow.Publisher<@NonNull ? extends CompletableSource> sources) Switches betweenCompletableSources emitted by the sourceFlow.Publisherwhenever a newCompletableSourceis emitted, disposing the previously runningCompletableSource, exposing the setup as aCompletablesequence and delaying all errors from all of them until all terminate.
- Backpressure:
- The
sourcesPublisheris consumed in an unbounded manner (requestingLong.MAX_VALUE). - Scheduler:
switchOnNextDelayErrordoes not operate by default on a particularScheduler.- Error handling:
- The returned
Completablecollects all errors emitted by either thesourcesPublisheror any innerCompletableSourceand emits them as aCompositeExceptionwhen all sources terminate. If only one source ever failed, its error is emitted as-is at the end.
- Parameters:
sources- thePublishersequence of innerCompletableSources to switch between- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifsourcesisnull- Since:
- 3.0.0
- See Also:
-
using
@CheckReturnValue @SchedulerSupport("none") @NonNull public static <@NonNull R> @NonNull Completable using(@NonNull @NonNull Supplier<@NonNull R> resourceSupplier, @NonNull @NonNull Function<? super @NonNull R, ? extends CompletableSource> sourceSupplier, @NonNull @NonNull Consumer<? super @NonNull R> resourceCleanup) Returns aCompletableinstance which manages a resource along with a customCompletableSourceinstance while the subscription is active.
This overload disposes eagerly before the terminal event is emitted.
- Scheduler:
usingdoes not operate by default on a particularScheduler.
- Type Parameters:
R- the resource type- Parameters:
resourceSupplier- theSupplierthat returns a resource to be managed.sourceSupplier- theFunctionthat given a resource returns aCompletableSourceinstance that will be subscribed toresourceCleanup- theConsumerthat disposes the resource created by the resource supplier- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifresourceSupplier,sourceSupplierorresourceCleanupisnull
-
using
@CheckReturnValue @NonNull @SchedulerSupport("none") public static <@NonNull R> @NonNull Completable using(@NonNull @NonNull Supplier<@NonNull R> resourceSupplier, @NonNull @NonNull Function<? super @NonNull R, ? extends CompletableSource> sourceSupplier, @NonNull @NonNull Consumer<? super @NonNull R> resourceCleanup, boolean eager) Returns aCompletableinstance which manages a resource along with a customCompletableSourceinstance while the subscription is active and performs eager or lazy resource disposition.
If this overload performs a lazy disposal after the terminal event is emitted. The exceptions thrown at this time will be delivered to the global
RxJavaPlugins.onError(Throwable)handler only.- Scheduler:
usingdoes not operate by default on a particularScheduler.
- Type Parameters:
R- the resource type- Parameters:
resourceSupplier- theSupplierthat returns a resource to be managedsourceSupplier- theFunctionthat given a resource returns a non-nullCompletableSourceinstance that will be subscribed toresourceCleanup- theConsumerthat disposes the resource created by the resource suppliereager- Iftruethen resource disposal will happen either on adispose()call before the upstream is disposed or just before the emission of a terminal event (onCompleteoronError). Iffalsethe resource disposal will happen either on adispose()call after the upstream is disposed or just after the emission of a terminal event (onCompleteoronError).- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifresourceSupplier,sourceSupplierorresourceCleanupisnull
-
wrap
@CheckReturnValue @NonNull @SchedulerSupport("none") public static @NonNull Completable wrap(@NonNull @NonNull CompletableSource source) Wraps the givenCompletableSourceinto aCompletableif not alreadyCompletable.
- Scheduler:
wrapdoes not operate by default on a particularScheduler.
- Parameters:
source- the source to wrap- Returns:
- the new wrapped or cast
Completableinstance - Throws:
NullPointerException- ifsourceisnull
-
ambWith
@CheckReturnValue @NonNull @SchedulerSupport("none") public final @NonNull Completable ambWith(@NonNull @NonNull CompletableSource other) Returns aCompletablethat emits the a terminated event of either thisCompletableor the otherCompletableSource, whichever fires first.
- Scheduler:
ambWithdoes not operate by default on a particularScheduler.
- Parameters:
other- the otherCompletableSource, notnull. A subscription to this provided source will occur after subscribing to the current source.- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifotherisnull
-
andThen
@CheckReturnValue @NonNull @SchedulerSupport("none") public final <@NonNull T> @NonNull Observable<T> andThen(@NonNull @NonNull ObservableSource<@NonNull T> next) Returns anObservablewhich will subscribe to thisCompletableand once that is completed then will subscribe to thenextObservableSource. An error event from thisCompletablewill be propagated to the downstream observer and will result in skipping the subscription to the nextObservableSource.
- Scheduler:
andThendoes not operate by default on a particularScheduler.
- Type Parameters:
T- the value type of the nextObservableSource- Parameters:
next- theObservableSourceto subscribe after thisCompletableis completed, notnull- Returns:
- the new
Observablethat composes thisCompletableand the nextObservableSource - Throws:
NullPointerException- ifnextisnull
-
andThen
@CheckReturnValue @NonNull @BackpressureSupport(FULL) @SchedulerSupport("none") public final <@NonNull T> @NonNull Flowable<T> andThen(@NonNull @NonNull Flow.Publisher<@NonNull T> next) Returns aFlowablewhich will subscribe to thisCompletableand once that is completed then will subscribe to thenextFlow.Publisher. An error event from thisCompletablewill be propagated to the downstream subscriber and will result in skipping the subscription to the nextPublisher.
- Backpressure:
- The returned
Flowablehonors the backpressure of the downstream consumer and expects the otherPublisherto honor it as well. - Scheduler:
andThendoes not operate by default on a particularScheduler.
- Type Parameters:
T- the value type of the nextPublisher- Parameters:
next- thePublisherto subscribe after thisCompletableis completed, notnull- Returns:
- the new
Flowablethat composes thisCompletableand the nextPublisher - Throws:
NullPointerException- ifnextisnull
-
andThen
@CheckReturnValue @NonNull @SchedulerSupport("none") public final <@NonNull T> @NonNull Single<T> andThen(@NonNull @NonNull SingleSource<@NonNull T> next) Returns aSinglewhich will subscribe to thisCompletableand once that is completed then will subscribe to thenextSingleSource. An error event from thisCompletablewill be propagated to the downstream observer and will result in skipping the subscription to the nextSingleSource.
- Scheduler:
andThendoes not operate by default on a particularScheduler.
- Type Parameters:
T- the value type of the nextSingleSource- Parameters:
next- theSingleSourceto subscribe after thisCompletableis completed, notnull- Returns:
- the new
Singlethat composes thisCompletableand the nextSingleSource - Throws:
NullPointerException- ifnextisnull
-
andThen
@CheckReturnValue @NonNull @SchedulerSupport("none") public final <@NonNull T> @NonNull Maybe<T> andThen(@NonNull @NonNull MaybeSource<@NonNull T> next) Returns aMaybewhich will subscribe to thisCompletableand once that is completed then will subscribe to thenextMaybeSource. An error event from thisCompletablewill be propagated to the downstream observer and will result in skipping the subscription to the nextMaybeSource.
- Scheduler:
andThendoes not operate by default on a particularScheduler.
- Type Parameters:
T- the value type of the nextMaybeSource- Parameters:
next- theMaybeSourceto subscribe after thisCompletableis completed, notnull- Returns:
- the new
Maybethat composes thisCompletableand the nextMaybeSource - Throws:
NullPointerException- ifnextisnull
-
andThen
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull Completable andThen(@NonNull @NonNull CompletableSource next) Returns aCompletablethat first runs thisCompletableand then the otherCompletableSource. An error event from thisCompletablewill be propagated to the downstream observer and will result in skipping the subscription to the nextCompletableSource.
This is an alias for
concatWith(CompletableSource).- Scheduler:
andThendoes not operate by default on a particularScheduler.
- Parameters:
next- the otherCompletableSource, notnull- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifnextisnull
-
blockingAwait
Subscribes to and awaits the termination of thisCompletableinstance in a blocking manner and rethrows any exception emitted.
- Scheduler:
blockingAwaitdoes not operate by default on a particularScheduler.- Error handling:
- If the source signals an error, the operator wraps a checked
ExceptionintoRuntimeExceptionand throws that. Otherwise,RuntimeExceptions andErrors are rethrown as they are.
- Throws:
RuntimeException- wrapping anInterruptedExceptionif the current thread is interrupted
-
blockingAwait
@CheckReturnValue @SchedulerSupport("none") public final boolean blockingAwait(long timeout, @NonNull @NonNull TimeUnit unit) Subscribes to and awaits the termination of thisCompletableinstance in a blocking manner with a specific timeout and rethrows any exception emitted within the timeout window.
- Scheduler:
blockingAwaitdoes not operate by default on a particularScheduler.- Error handling:
- If the source signals an error, the operator wraps a checked
ExceptionintoRuntimeExceptionand throws that. Otherwise,RuntimeExceptions andErrors are rethrown as they are.
- Parameters:
timeout- the timeout valueunit- the timeout unit- Returns:
trueif the thisCompletableinstance completed normally within the time limit,falseif the timeout elapsed before thisCompletableterminated.- Throws:
RuntimeException- wrapping anInterruptedExceptionif the current thread is interruptedNullPointerException- ifunitisnull
-
blockingSubscribe
Subscribes to the currentCompletableand blocks the current thread until it terminates.
- Scheduler:
blockingSubscribedoes not operate by default on a particularScheduler.- Error handling:
- If the current
Completablesignals an error, theThrowableis routed to the global error handler viaRxJavaPlugins.onError(Throwable). If the current thread is interrupted, anInterruptedExceptionis routed to the same global error handler.
- Since:
- 3.0.0
- See Also:
-
blockingSubscribe
Subscribes to the currentCompletableand calls givenonCompletecallback on the current thread when it completes normally.
- Scheduler:
blockingSubscribedoes not operate by default on a particularScheduler.- Error handling:
- If either the current
Completablesignals an error oronCompletethrows, the respectiveThrowableis routed to the global error handler viaRxJavaPlugins.onError(Throwable). If the current thread is interrupted, anInterruptedExceptionis routed to the same global error handler.
- Parameters:
onComplete- theActionto call if the currentCompletablecompletes normally- Throws:
NullPointerException- ifonCompleteisnull- Since:
- 3.0.0
- See Also:
-
blockingSubscribe
@SchedulerSupport("none") public final void blockingSubscribe(@NonNull @NonNull Action onComplete, @NonNull @NonNull Consumer<? super Throwable> onError) Subscribes to the currentCompletableand calls the appropriate callback on the current thread when it terminates.
- Scheduler:
blockingSubscribedoes not operate by default on a particularScheduler.- Error handling:
- If either
onCompleteoronErrorthrow, theThrowableis routed to the global error handler viaRxJavaPlugins.onError(Throwable). If the current thread is interrupted, theonErrorconsumer is called with anInterruptedException.
- Parameters:
onComplete- theActionto call if the currentCompletablecompletes normallyonError- theConsumerto call if the currentCompletablesignals an error- Throws:
NullPointerException- ifonCompleteoronErrorisnull- Since:
- 3.0.0
-
blockingSubscribe
@SchedulerSupport("none") public final void blockingSubscribe(@NonNull @NonNull CompletableObserver observer) Subscribes to the currentCompletableand calls the appropriateCompletableObservermethod on the current thread.
- Scheduler:
blockingSubscribedoes not operate by default on a particularScheduler.- Error handling:
- An
onErrorsignal is delivered to theCompletableObserver.onError(Throwable)method. If any of theCompletableObserver's methods throw, theRuntimeExceptionis propagated to the caller of this method. If the current thread is interrupted, anInterruptedExceptionis delivered toobserver.onError.
- Parameters:
observer- theCompletableObserverto call methods on the current thread- Throws:
NullPointerException- ifobserverisnull- Since:
- 3.0.0
-
cache
Subscribes to thisCompletableonly once, when the firstCompletableObserversubscribes to the resultCompletable, caches its terminal event and relays/replays it to observers.
Note that this operator doesn't allow disposing the connection of the upstream source.
- Scheduler:
cachedoes not operate by default on a particularScheduler.
History: 2.0.4 - experimental
- Returns:
- the new
Completableinstance - Since:
- 2.1
-
compose
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull Completable compose(@NonNull @NonNull CompletableTransformer transformer) Calls the given transformer function with this instance and returns the function's resultingCompletableSourcewrapped withwrap(CompletableSource).
- Scheduler:
composedoes not operate by default on a particularScheduler.
- Parameters:
transformer- the transformer function, notnull- Returns:
- the new
Completableinstance - Throws:
NullPointerException- iftransformerisnull
-
concatWith
@CheckReturnValue @NonNull @SchedulerSupport("none") public final @NonNull Completable concatWith(@NonNull @NonNull CompletableSource other) Concatenates thisCompletablewith anotherCompletableSource. An error event from thisCompletablewill be propagated to the downstream observer and will result in skipping the subscription to the nextCompletableSource.
- Scheduler:
concatWithdoes not operate by default on a particularScheduler.
- Parameters:
other- the otherCompletableSource, notnull- Returns:
- the new
Completablewhich subscribes to this and then the otherCompletableSource - Throws:
NullPointerException- ifotherisnull- See Also:
-
delay
@CheckReturnValue @SchedulerSupport("io.reactivex:computation") @NonNull public final @NonNull Completable delay(long time, @NonNull @NonNull TimeUnit unit) Returns aCompletablewhich delays the emission of the completion event by the given time.
- Scheduler:
delaydoes operate by default on thecomputationScheduler.
- Parameters:
time- the delay timeunit- the delay unit- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifunitisnull
-
delay
@CheckReturnValue @SchedulerSupport("custom") @NonNull public final @NonNull Completable delay(long time, @NonNull @NonNull TimeUnit unit, @NonNull @NonNull Scheduler scheduler) Returns aCompletablewhich delays the emission of the completion event by the given time while running on the specifiedScheduler.
- Scheduler:
delayoperates on theScheduleryou specify.
- Parameters:
time- the delay timeunit- the delay unitscheduler- theSchedulerto run the delayed completion on- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifunitorschedulerisnull
-
delay
@CheckReturnValue @NonNull @SchedulerSupport("custom") public final @NonNull Completable delay(long time, @NonNull @NonNull TimeUnit unit, @NonNull @NonNull Scheduler scheduler, boolean delayError) Returns aCompletablewhich delays the emission of the completion event, and optionally the error as well, by the given time while running on the specifiedScheduler.
- Scheduler:
delayoperates on theScheduleryou specify.
- Parameters:
time- the delay timeunit- the delay unitscheduler- theSchedulerto run the delayed completion ondelayError- delay the error emission as well?- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifunitorschedulerisnull
-
delaySubscription
@CheckReturnValue @SchedulerSupport("io.reactivex:computation") @NonNull public final @NonNull Completable delaySubscription(long time, @NonNull @NonNull TimeUnit unit) Returns aCompletablethat delays the subscription to the upstream by a given amount of time.
- Scheduler:
- This version of
delaySubscriptionoperates by default on thecomputationScheduler.
History: 2.2.3 - experimental
- Parameters:
time- the time to delay the subscriptionunit- the time unit ofdelay- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifunitisnull- Since:
- 3.0.0
- See Also:
-
delaySubscription
@CheckReturnValue @SchedulerSupport("custom") @NonNull public final @NonNull Completable delaySubscription(long time, @NonNull @NonNull TimeUnit unit, @NonNull @NonNull Scheduler scheduler) Returns aCompletablethat delays the subscription to the upstream by a given amount of time, both waiting and subscribing on a givenScheduler.
- Scheduler:
- You specify which
Schedulerthis operator will use.
History: 2.2.3 - experimental
- Parameters:
time- the time to delay the subscriptionunit- the time unit ofdelayscheduler- theScheduleron which the waiting and subscription will happen- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifunitorschedulerisnull- Since:
- 3.0.0
- See Also:
-
doOnComplete
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull Completable doOnComplete(@NonNull @NonNull Action onComplete) Returns aCompletablewhich calls the givenonCompleteActionif thisCompletablecompletes.
- Scheduler:
doOnCompletedoes not operate by default on a particularScheduler.
- Parameters:
onComplete- theActionto call when this emits anonCompleteevent- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifonCompleteisnull- See Also:
-
doOnDispose
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull Completable doOnDispose(@NonNull @NonNull Action onDispose) Calls the sharedActionif aCompletableObserversubscribed to the currentCompletabledisposes the commonDisposableit received viaonSubscribe.
- Scheduler:
doOnDisposedoes not operate by default on a particularScheduler.
- Parameters:
onDispose- theActionto call when the downstream observer disposes the subscription- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifonDisposeisnull
-
doOnError
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull Completable doOnError(@NonNull @NonNull Consumer<? super Throwable> onError) Returns aCompletablewhich calls the givenonErrorConsumerif thisCompletableemits an error.
- Scheduler:
doOnErrordoes not operate by default on a particularScheduler.
- Parameters:
onError- the errorConsumerreceiving the upstreamThrowableif the upstream signals it viaonError- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifonErrorisnull- See Also:
-
doOnEvent
@CheckReturnValue @NonNull @SchedulerSupport("none") public final @NonNull Completable doOnEvent(@NonNull @NonNull Consumer<@Nullable ? super Throwable> onEvent) Returns aCompletablewhich calls the givenonEventConsumerwith theThrowablefor anonErrorornullfor anonCompletesignal from thisCompletablebefore delivering the signal to the downstream.
- Scheduler:
doOnEventdoes not operate by default on a particularScheduler.
- Parameters:
onEvent- the eventConsumerthat receivesnullfor upstream completion or aThrowableif the upstream signaled an error- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifonEventisnull
-
doOnLifecycle
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull Completable doOnLifecycle(@NonNull @NonNull Consumer<? super Disposable> onSubscribe, @NonNull @NonNull Action onDispose) Calls the appropriateonXXXmethod (shared between allCompletableObservers) for the lifecycle events of the sequence (subscription, disposal).
- Scheduler:
doOnLifecycledoes not operate by default on a particularScheduler.
- Parameters:
onSubscribe- aConsumercalled with theDisposablesent viaCompletableObserver.onSubscribe(Disposable)onDispose- called when the downstream disposes theDisposableviadispose()- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifonSubscribeoronDisposeisnull- Since:
- 3.0.0
- See Also:
-
doOnSubscribe
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull Completable doOnSubscribe(@NonNull @NonNull Consumer<? super Disposable> onSubscribe) Returns aCompletableinstance that calls the givenonSubscribecallback with the disposable that the downstreamCompletableObservers receive upon subscription.
- Scheduler:
doOnSubscribedoes not operate by default on a particularScheduler.
- Parameters:
onSubscribe- theConsumercalled when a downstreamCompletableObserversubscribes- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifonSubscribeisnull
-
doOnTerminate
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull Completable doOnTerminate(@NonNull @NonNull Action onTerminate) Returns aCompletableinstance that calls the givenonTerminateActionjust before thisCompletablecompletes normally or with an exception.
- Scheduler:
doOnTerminatedoes not operate by default on a particularScheduler.
- Parameters:
onTerminate- theActionto call just before thisCompletableterminates- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifonTerminateisnull- See Also:
-
doAfterTerminate
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull Completable doAfterTerminate(@NonNull @NonNull Action onAfterTerminate) Returns aCompletableinstance that calls the givenonAfterTerminateActionafter thisCompletablecompletes normally or with an exception.
- Scheduler:
doAfterTerminatedoes not operate by default on a particularScheduler.
- Parameters:
onAfterTerminate- theActionto call after thisCompletableterminates- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifonAfterTerminateisnull- See Also:
-
doFinally
@CheckReturnValue @NonNull @SchedulerSupport("none") public final @NonNull Completable doFinally(@NonNull @NonNull Action onFinally) Calls the specifiedActionafter thisCompletablesignalsonErrororonCompleteor gets disposed by the downstream.
In case of a race between a terminal event and a dispose call, the provided
onFinallyaction is executed once per subscription.Note that the
onFinallyaction is shared between subscriptions and as such should be thread-safe.- Scheduler:
doFinallydoes not operate by default on a particularScheduler.
History: 2.0.1 - experimental
- Parameters:
onFinally- theActioncalled when thisCompletableterminates or gets disposed- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifonFinallyisnull- Since:
- 2.1
-
lift
@CheckReturnValue @NonNull @SchedulerSupport("none") public final @NonNull Completable lift(@NonNull @NonNull CompletableOperator onLift) This method requires advanced knowledge about building operators, please consider other standard composition methods first; Returns aCompletablewhich, when subscribed to, invokes theapply(CompletableObserver)method of the providedCompletableOperatorfor each individual downstreamCompletableand allows the insertion of a custom operator by accessing the downstream'sCompletableObserverduring this subscription phase and providing a newCompletableObserver, containing the custom operator's intended business logic, that will be used in the subscription process going further upstream.
Generally, such a new
CompletableObserverwill wrap the downstream'sCompletableObserverand forwards theonErrorandonCompleteevents from the upstream directly or according to the emission pattern the custom operator's business logic requires. In addition, such operator can intercept the flow control calls ofdisposeandisDisposedthat would have traveled upstream and perform additional actions depending on the same business logic requirements.Example:
// Step 1: Create the consumer type that will be returned by the CompletableOperator.apply(): public final class CustomCompletableObserver implements CompletableObserver, Disposable { // The downstream's CompletableObserver that will receive the onXXX events final CompletableObserver downstream; // The connection to the upstream source that will call this class' onXXX methods Disposable upstream; // The constructor takes the downstream subscriber and usually any other parameters public CustomCompletableObserver(CompletableObserver downstream) { this.downstream = downstream; } // In the subscription phase, the upstream sends a Disposable to this class // and subsequently this class has to send a Disposable to the downstream. // Note that relaying the upstream's Disposable directly is not allowed in RxJava @Override public void onSubscribe(Disposable d) { if (upstream != null) { d.dispose(); } else { upstream = d; downstream.onSubscribe(this); } } // Some operators may handle the upstream's error while others // could just forward it to the downstream. @Override public void onError(Throwable throwable) { downstream.onError(throwable); } // When the upstream completes, usually the downstream should complete as well. // In completable, this could also mean doing some side-effects @Override public void onComplete() { System.out.println("Sequence completed"); downstream.onComplete(); } // Some operators may use their own resources which should be cleaned up if // the downstream disposes the flow before it completed. Operators without // resources can simply forward the dispose to the upstream. // In some cases, a disposed flag may be set by this method so that other parts // of this class may detect the dispose and stop sending events // to the downstream. @Override public void dispose() { upstream.dispose(); } // Some operators may simply forward the call to the upstream while others // can return the disposed flag set in dispose(). @Override public boolean isDisposed() { return upstream.isDisposed(); } } // Step 2: Create a class that implements the CompletableOperator interface and // returns the custom consumer type from above in its apply() method. // Such class may define additional parameters to be submitted to // the custom consumer type. final class CustomCompletableOperator implements CompletableOperator { @Override public CompletableObserver apply(CompletableObserver upstream) { return new CustomCompletableObserver(upstream); } } // Step 3: Apply the custom operator via lift() in a flow by creating an instance of it // or reusing an existing one. Completable.complete() .lift(new CustomCompletableOperator()) .test() .assertResult();Creating custom operators can be complicated and it is recommended one consults the RxJava wiki: Writing operators page about the tools, requirements, rules, considerations and pitfalls of implementing them.
Note that implementing custom operators via this
lift()method adds slightly more overhead by requiring an additional allocation and indirection per assembled flows. Instead, extending the abstractCompletableclass and creating aCompletableTransformerwith it is recommended.Note also that it is not possible to stop the subscription phase in
lift()as theapply()method requires a non-nullCompletableObserverinstance to be returned, which is then unconditionally subscribed to the currentCompletable. For example, if the operator decided there is no reason to subscribe to the upstream source because of some optimization possibility or a failure to prepare the operator, it still has to return aCompletableObserverthat should immediately dispose the upstream'sDisposablein itsonSubscribemethod. Again, using aCompletableTransformerand extending theCompletableis a better option assubscribeActual(CompletableObserver)can decide to not subscribe to its upstream after all.- Scheduler:
liftdoes not operate by default on a particularScheduler, however, theCompletableOperatormay use aSchedulerto support its own asynchronous behavior.
- Parameters:
onLift- theCompletableOperatorthat receives the downstream'sCompletableObserverand should return aCompletableObserverwith custom behavior to be used as the consumer for the currentCompletable.- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifonLiftisnull- See Also:
-
materialize
@CheckReturnValue @SchedulerSupport("none") @NonNull public final <@NonNull T> @NonNull Single<Notification<T>> materialize()Maps the signal types of thisCompletableinto aNotificationof the same kind and emits it as a single success value to downstream.
- Scheduler:
materializedoes not operate by default on a particularScheduler.
History: 2.2.4 - experimental
- Type Parameters:
T- the intended target element type of theNotification- Returns:
- the new
Singleinstance - Since:
- 3.0.0
- See Also:
-
mergeWith
@CheckReturnValue @NonNull @SchedulerSupport("none") public final @NonNull Completable mergeWith(@NonNull @NonNull CompletableSource other) Returns aCompletablewhich subscribes to this and the otherCompletableSourceand completes when both of them complete or one emits an error.
- Scheduler:
mergeWithdoes not operate by default on a particularScheduler.
- Parameters:
other- the otherCompletableSourceinstance- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifotherisnull
-
observeOn
@CheckReturnValue @NonNull @SchedulerSupport("custom") public final @NonNull Completable observeOn(@NonNull @NonNull Scheduler scheduler) Returns aCompletablewhich emits the terminal events from the thread of the specifiedScheduler.
- Scheduler:
observeOnoperates on aScheduleryou specify.
- Parameters:
scheduler- theSchedulerto emit terminal events on- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifschedulerisnull
-
onErrorComplete
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull Completable onErrorComplete()Returns aCompletableinstance that if thisCompletableemits an error, it will emit anonCompleteand swallow the upstreamThrowable.
- Scheduler:
onErrorCompletedoes not operate by default on a particularScheduler.
- Returns:
- the new
Completableinstance
-
onErrorComplete
@CheckReturnValue @NonNull @SchedulerSupport("none") public final @NonNull Completable onErrorComplete(@NonNull @NonNull Predicate<? super Throwable> predicate) Returns aCompletableinstance that if thisCompletableemits an error and thePredicatereturnstrue, it will emit anonCompleteand swallow theThrowable.
- Scheduler:
onErrorCompletedoes not operate by default on a particularScheduler.
- Parameters:
predicate- thePredicateto call when aThrowableis emitted which should returntrueif theThrowableshould be swallowed and replaced with anonComplete.- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifpredicateisnull
-
onErrorResumeNext
@CheckReturnValue @NonNull @SchedulerSupport("none") public final @NonNull Completable onErrorResumeNext(@NonNull @NonNull Function<? super Throwable, ? extends CompletableSource> fallbackSupplier) Returns aCompletableinstance that when encounters an error from thisCompletable, calls the specifiedmapperFunctionthat returns aCompletableSourceinstance for it and resumes the execution with it.
- Scheduler:
onErrorResumeNextdoes not operate by default on a particularScheduler.
- Parameters:
fallbackSupplier- themapperFunctionthat takes the error and should return aCompletableSourceas continuation.- Returns:
- the new
Completableinstance - Throws:
NullPointerException- iffallbackSupplierisnull
-
onErrorResumeWith
@CheckReturnValue @NonNull @SchedulerSupport("none") public final @NonNull Completable onErrorResumeWith(@NonNull @NonNull CompletableSource fallback) Resumes the flow with the givenCompletableSourcewhen the currentCompletablefails instead of signaling the error viaonError.
You can use this to prevent errors from propagating or to supply fallback data should errors be encountered.
- Scheduler:
onErrorResumeWithdoes not operate by default on a particularScheduler.
- Parameters:
fallback- the nextCompletableSourcethat will take over if the currentCompletableencounters an error- Returns:
- the new
Completableinstance - Throws:
NullPointerException- iffallbackisnull- Since:
- 3.0.0
- See Also:
-
onErrorReturn
@CheckReturnValue @NonNull @SchedulerSupport("none") public final <@NonNull T> @NonNull Maybe<T> onErrorReturn(@NonNull @NonNull Function<? super Throwable, ? extends @NonNull T> itemSupplier) Ends the flow with a success item returned by a function for theThrowableerror signaled by the currentCompletableinstead of signaling the error viaonError.
You can use this to prevent errors from propagating or to supply fallback data should errors be encountered.
- Scheduler:
onErrorReturndoes not operate by default on a particularScheduler.
- Type Parameters:
T- the item type to return on error- Parameters:
itemSupplier- a function that returns a single value that will be emitted as success value the currentCompletablesignals anonErrorevent- Returns:
- the new
Maybeinstance - Throws:
NullPointerException- ifitemSupplierisnull- Since:
- 3.0.0
- See Also:
-
onErrorReturnItem
@CheckReturnValue @NonNull @SchedulerSupport("none") public final <@NonNull T> @NonNull Maybe<T> onErrorReturnItem(@NonNull @NonNull T item) Ends the flow with the given success item when the currentCompletablefails instead of signaling the error viaonError.
You can use this to prevent errors from propagating or to supply fallback data should errors be encountered.
- Scheduler:
onErrorReturnItemdoes not operate by default on a particularScheduler.
- Type Parameters:
T- the item type to return on error- Parameters:
item- the value that is emitted asonSuccessin case the currentCompletablesignals anonError- Returns:
- the new
Maybeinstance - Throws:
NullPointerException- ifitemisnull- See Also:
-
onTerminateDetach
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull Completable onTerminateDetach()Nulls out references to the upstream producer and downstreamCompletableObserverif the sequence is terminated or downstream callsdispose().
- Scheduler:
onTerminateDetachdoes not operate by default on a particularScheduler.
History: 2.1.5 - experimental
- Returns:
- the new
Completableinstance - Since:
- 2.2
-
repeat
Returns aCompletablethat repeatedly subscribes to thisCompletableuntil disposed.
- Scheduler:
repeatdoes not operate by default on a particularScheduler.
- Returns:
- the new
Completableinstance
-
repeat
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull Completable repeat(long times) Returns aCompletablethat subscribes repeatedly at most the given number of times to thisCompletable.
- Scheduler:
repeatdoes not operate by default on a particularScheduler.
- Parameters:
times- the number of times the re-subscription should happen- Returns:
- the new
Completableinstance - Throws:
IllegalArgumentException- iftimesis negative
-
repeatUntil
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull Completable repeatUntil(@NonNull @NonNull BooleanSupplier stop) Returns aCompletablethat repeatedly subscribes to thisCompletableso long as the given stopBooleanSupplierreturnsfalse.
- Scheduler:
repeatUntildoes not operate by default on a particularScheduler.
- Parameters:
stop- theBooleanSupplierthat should returntrueto stop resubscribing.- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifstopisnull
-
repeatWhen
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull Completable repeatWhen(@NonNull @NonNull Function<? super Flowable<Object>, @NonNull ? extends Flow.Publisher<@NonNull ?>> handler) Returns aCompletableinstance that repeats when theFlow.Publisherreturned by the handlerFunctionemits an item or completes when thisPublisheremits anonCompleteevent.
- Scheduler:
repeatWhendoes not operate by default on a particularScheduler.
- Parameters:
handler- theFunctionthat transforms the stream of values indicating the completion of thisCompletableand returns aPublisherthat emits items for repeating or completes to indicate the repetition should stop- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifhandlerisnull
-
retry
Returns aCompletablethat retries thisCompletableas long as it emits anonErrorevent.
- Scheduler:
retrydoes not operate by default on a particularScheduler.
- Returns:
- the new
Completableinstance
-
retry
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull Completable retry(@NonNull @NonNull BiPredicate<? super Integer, ? super Throwable> predicate) Returns aCompletablethat retries thisCompletablein case of an error as long as thepredicatereturnstrue.
- Scheduler:
retrydoes not operate by default on a particularScheduler.
- Parameters:
predicate- thePredicatecalled when thisCompletableemits an error with the repeat count and the latestThrowableand should returntrueto retry.- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifpredicateisnull
-
retry
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull Completable retry(long times) Returns aCompletablethat when thisCompletableemits an error, retries at most the given number of times before giving up and emitting the last error.
- Scheduler:
retrydoes not operate by default on a particularScheduler.
- Parameters:
times- the number of times to resubscribe if the currentCompletablefails- Returns:
- the new
Completableinstance - Throws:
IllegalArgumentException- iftimesis negative
-
retry
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull Completable retry(long times, @NonNull @NonNull Predicate<? super Throwable> predicate) Returns aCompletablethat when thisCompletableemits an error, retries at most times or until the predicate returnsfalse, whichever happens first and emitting the last error.
- Scheduler:
retrydoes not operate by default on a particularScheduler.
History: 2.1.8 - experimental
- Parameters:
times- the number of times to resubscribe if the currentCompletablefailspredicate- thePredicatethat is called with the latestThrowableand should returntrueto indicate the returnedCompletableshould resubscribe to thisCompletable.- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifpredicateisnullIllegalArgumentException- iftimesis negative- Since:
- 2.2
-
retry
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull Completable retry(@NonNull @NonNull Predicate<? super Throwable> predicate) Returns aCompletablethat when thisCompletableemits an error, calls the given predicate with the latestThrowableto decide whether to resubscribe to the upstream or not.
- Scheduler:
retrydoes not operate by default on a particularScheduler.
- Parameters:
predicate- thePredicatethat is called with the latestThrowableand should returntrueto indicate the returnedCompletableshould resubscribe to thisCompletable.- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifpredicateisnull
-
retryUntil
@CheckReturnValue @NonNull @SchedulerSupport("none") public final @NonNull Completable retryUntil(@NonNull @NonNull BooleanSupplier stop) Retries until the given stop function returnstrue.
- Scheduler:
retryUntildoes not operate by default on a particularScheduler.
- Parameters:
stop- the function that should returntrueto stop retrying- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifstopisnull- Since:
- 3.0.0
-
retryWhen
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull Completable retryWhen(@NonNull @NonNull Function<? super Flowable<Throwable>, @NonNull ? extends Flow.Publisher<@NonNull ?>> handler) Returns aCompletablewhich given aFlow.Publisherand when thisCompletableemits an error, delivers that error through aFlowableand thePublishershould signal a value indicating a retry in response or a terminal event indicating a termination.
Note that the inner
Publisherreturned by the handler function should signal eitheronNext,onErrororonCompletein response to the receivedThrowableto indicate the operator should retry or terminate. If the upstream to the operator is asynchronous, signalingonNextfollowed byonCompleteimmediately may result in the sequence to be completed immediately. Similarly, if this innerPublishersignalsonErrororonCompletewhile the upstream is active, the sequence is terminated with the same signal immediately.The following example demonstrates how to retry an asynchronous source with a delay:
Completable.timer(1, TimeUnit.SECONDS) .doOnSubscribe(s -> System.out.println("subscribing")) .doOnComplete(() -> { throw new RuntimeException(); }) .retryWhen(errors -> { AtomicInteger counter = new AtomicInteger(); return errors .takeWhile(e -> counter.getAndIncrement() != 3) .flatMap(e -> { System.out.println("delay retry by " + counter.get() + " second(s)"); return Flowable.timer(counter.get(), TimeUnit.SECONDS); }); }) .blockingAwait();- Scheduler:
retryWhendoes not operate by default on a particularScheduler.
- Parameters:
handler- theFunctionthat receives aFlowabledeliveringThrowables and should return aPublisherthat emits items to indicate retries or emits terminal events to indicate termination.- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifhandlerisnull
-
safeSubscribe
@SchedulerSupport("none") public final void safeSubscribe(@NonNull @NonNull CompletableObserver observer) Wraps the givenCompletableObserver, catches anyRuntimeExceptions thrown by itsCompletableObserver.onSubscribe(Disposable),CompletableObserver.onError(Throwable)orCompletableObserver.onComplete()methods and routes those to the global error handler viaRxJavaPlugins.onError(Throwable).By default, the
Completableprotocol forbids theonXXXmethods to throw, but someCompletableObserverimplementation may do it anyway, causing undefined behavior in the upstream. This method and the underlying safe wrapper ensures such misbehaving consumers don't disrupt the protocol.- Scheduler:
safeSubscribedoes not operate by default on a particularScheduler.
- Parameters:
observer- the potentially misbehavingCompletableObserver- Throws:
NullPointerException- ifobserverisnull- Since:
- 3.0.0
- See Also:
-
startWith
@CheckReturnValue @NonNull @SchedulerSupport("none") public final @NonNull Completable startWith(@NonNull @NonNull CompletableSource other) Returns aCompletablewhich first runs the otherCompletableSourcethen the currentCompletableif the other completed normally.
- Scheduler:
startWithdoes not operate by default on a particularScheduler.
- Parameters:
other- the otherCompletableSourceto run first- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifotherisnull
-
startWith
@CheckReturnValue @NonNull @SchedulerSupport("none") @BackpressureSupport(FULL) public final <@NonNull T> @NonNull Flowable<T> startWith(@NonNull @NonNull SingleSource<@NonNull T> other) Returns aFlowablewhich first runs the otherSingleSourcethen the currentCompletableif the other succeeded normally.
- Backpressure:
- The returned
Flowablehonors the backpressure of the downstream consumer. - Scheduler:
startWithdoes not operate by default on a particularScheduler.
- Type Parameters:
T- the element type of theotherSingleSource.- Parameters:
other- the otherSingleSourceto run first- Returns:
- the new
Flowableinstance - Throws:
NullPointerException- ifotherisnull- Since:
- 3.0.0
-
startWith
@CheckReturnValue @NonNull @SchedulerSupport("none") @BackpressureSupport(FULL) public final <@NonNull T> @NonNull Flowable<T> startWith(@NonNull @NonNull MaybeSource<@NonNull T> other) Returns aFlowablewhich first runs the otherMaybeSourcethen the currentCompletableif the other succeeded or completed normally.
- Backpressure:
- The returned
Flowablehonors the backpressure of the downstream consumer. - Scheduler:
startWithdoes not operate by default on a particularScheduler.
- Type Parameters:
T- the element type of theotherMaybeSource.- Parameters:
other- the otherMaybeSourceto run first- Returns:
- the new
Flowableinstance - Throws:
NullPointerException- ifotherisnull- Since:
- 3.0.0
-
startWith
@CheckReturnValue @NonNull @SchedulerSupport("none") public final <@NonNull T> @NonNull Observable<T> startWith(@NonNull @NonNull ObservableSource<@NonNull T> other) Returns anObservablewhich first delivers the events of the otherObservableSourcethen runs the currentCompletable.
- Scheduler:
startWithdoes not operate by default on a particularScheduler.
- Type Parameters:
T- the value type- Parameters:
other- the otherObservableSourceto run first- Returns:
- the new
Observableinstance - Throws:
NullPointerException- ifotherisnull
-
startWith
@CheckReturnValue @NonNull @BackpressureSupport(FULL) @SchedulerSupport("none") public final <@NonNull T> @NonNull Flowable<T> startWith(@NonNull @NonNull Flow.Publisher<@NonNull T> other) Returns aFlowablewhich first delivers the events of the otherFlow.Publisherthen runs the currentCompletable.
- Backpressure:
- The returned
Flowablehonors the backpressure of the downstream consumer and expects the otherPublisherto honor it as well. - Scheduler:
startWithdoes not operate by default on a particularScheduler.
- Type Parameters:
T- the value type- Parameters:
other- the otherPublisherto run first- Returns:
- the new
Flowableinstance - Throws:
NullPointerException- ifotherisnull
-
hide
Hides the identity of thisCompletableand itsDisposable.
Allows preventing certain identity-based optimizations (fusion).
- Scheduler:
hidedoes not operate by default on a particularScheduler.
History: 2.0.5 - experimental
- Returns:
- the new
Completableinstance - Since:
- 2.1
-
subscribe
Subscribes to thisCompletableand returns aDisposablewhich can be used to dispose the subscription.
- Scheduler:
subscribedoes not operate by default on a particularScheduler.
- Returns:
- the new
Disposablethat can be used for disposing the subscription at any time - See Also:
-
subscribe
@SchedulerSupport("none") public final void subscribe(@NonNull @NonNull CompletableObserver observer) Description copied from interface:CompletableSourceSubscribes the givenCompletableObserverto thisCompletableSourceinstance.- Specified by:
subscribein interfaceCompletableSource- Parameters:
observer- theCompletableObserver, notnull
-
subscribeActual
Implement this method to handle the incomingCompletableObservers and perform the business logic in your operator.There is no need to call any of the plugin hooks on the current
Completableinstance or theCompletableObserver; all hooks and basic safeguards have been applied bysubscribe(CompletableObserver)before this method gets called.- Parameters:
observer- theCompletableObserverinstance, nevernull
-
subscribeWith
@CheckReturnValue @SchedulerSupport("none") @NonNull public final <@NonNull E extends CompletableObserver> E subscribeWith(@NonNull E observer) Subscribes a givenCompletableObserver(subclass) to thisCompletableand returns the givenCompletableObserveras is.
Usage example:
Completable source = Completable.complete().delay(1, TimeUnit.SECONDS); CompositeDisposable composite = new CompositeDisposable(); DisposableCompletableObserver ds = new DisposableCompletableObserver() { // ... }; composite.add(source.subscribeWith(ds));- Scheduler:
subscribeWithdoes not operate by default on a particularScheduler.
- Type Parameters:
E- the type of theCompletableObserverto use and return- Parameters:
observer- theCompletableObserver(subclass) to use and return, notnull- Returns:
- the input
observer - Throws:
NullPointerException- ifobserverisnull- Since:
- 2.0
-
subscribe
@CheckReturnValue @NonNull @SchedulerSupport("none") public final @NonNull Disposable subscribe(@NonNull @NonNull Action onComplete, @NonNull @NonNull Consumer<? super Throwable> onError) Subscribes to thisCompletableand calls back either theonErrororonCompletefunctions.
- Scheduler:
subscribedoes not operate by default on a particularScheduler.
- Parameters:
onComplete- theActionthat is called if theCompletablecompletes normallyonError- theConsumerthat is called if thisCompletableemits an error- Returns:
- the new
Disposablethat can be used for disposing the subscription at any time - Throws:
NullPointerException- ifonCompleteoronErrorisnull- See Also:
-
subscribe
@SchedulerSupport("none") @NonNull public final @NonNull Disposable subscribe(@NonNull @NonNull Action onComplete, @NonNull @NonNull Consumer<? super Throwable> onError, @NonNull @NonNull DisposableContainer container) Wraps the given onXXX callbacks into aDisposableCompletableObserver, adds it to the givenDisposableContainerand ensures, that if the upstream terminates or this particularDisposableis disposed, theCompletableObserveris removed from the given composite.The
CompletableObserverwill be removed after the callback for the terminal event has been invoked.- Scheduler:
subscribedoes not operate by default on a particularScheduler.
- Parameters:
onComplete- the callback for an upstream completiononError- the callback for an upstream errorcontainer- theDisposableContainer(such asCompositeDisposable) to add and remove the createdDisposableCompletableObserver- Returns:
- the
Disposablethat allows disposing the particular subscription. - Throws:
NullPointerException- ifonComplete,onErrororcontainerisnull- Since:
- 3.1.0
-
subscribe
@CheckReturnValue @NonNull @SchedulerSupport("none") public final @NonNull Disposable subscribe(@NonNull @NonNull Action onComplete) Subscribes to thisCompletableand calls the givenActionwhen thisCompletablecompletes normally.
If the
Completableemits an error, it is wrapped into anOnErrorNotImplementedExceptionand routed to the globalRxJavaPlugins.onError(Throwable)handler.- Scheduler:
subscribedoes not operate by default on a particularScheduler.
- Parameters:
onComplete- theActioncalled when thisCompletablecompletes normally- Returns:
- the new
Disposablethat can be used for disposing the subscription at any time - Throws:
NullPointerException- ifonCompleteisnull- See Also:
-
subscribeOn
@CheckReturnValue @NonNull @SchedulerSupport("custom") public final @NonNull Completable subscribeOn(@NonNull @NonNull Scheduler scheduler) Returns aCompletablewhich subscribes the downstream subscriber on the specified scheduler, making sure the subscription side-effects happen on that specific thread of theScheduler.
- Scheduler:
subscribeOnoperates on aScheduleryou specify.
- Parameters:
scheduler- theSchedulerto subscribe on- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifschedulerisnull
-
takeUntil
@CheckReturnValue @NonNull @SchedulerSupport("none") public final @NonNull Completable takeUntil(@NonNull @NonNull CompletableSource other) Terminates the downstream if this or the otherCompletableterminates (wins the termination race) while disposing the connection to the losing source.
- Scheduler:
takeUntildoes not operate by default on a particularScheduler.- Error handling:
- If both this and the other sources signal an error, only one of the errors
is signaled to the downstream and the other error is signaled to the global
error handler via
RxJavaPlugins.onError(Throwable).
History: 2.1.17 - experimental
- Parameters:
other- the other completable source to observe for the terminal signals- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifotherisnull- Since:
- 2.2
-
timeout
@CheckReturnValue @SchedulerSupport("io.reactivex:computation") @NonNull public final @NonNull Completable timeout(long timeout, @NonNull @NonNull TimeUnit unit) Returns aCompletable that runs thisCompletableand emits aTimeoutExceptionin case thisCompletabledoesn't complete within the given time.
- Scheduler:
timeoutsignals theTimeoutExceptionon thecomputationScheduler.
- Parameters:
timeout- the timeout valueunit- the unit oftimeout- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifunitisnull
-
timeout
@CheckReturnValue @NonNull @SchedulerSupport("io.reactivex:computation") public final @NonNull Completable timeout(long timeout, @NonNull @NonNull TimeUnit unit, @NonNull @NonNull CompletableSource fallback) Returns aCompletablethat runs thisCompletableand switches to the otherCompletableSourcein case thisCompletabledoesn't complete within the given time.
- Scheduler:
timeoutsubscribes to the otherCompletableSourceon thecomputationScheduler.
- Parameters:
timeout- the timeout valueunit- the unit oftimeoutfallback- the otherCompletableSourceinstance to switch to in case of a timeout- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifunitorfallbackisnull
-
timeout
@CheckReturnValue @SchedulerSupport("custom") @NonNull public final @NonNull Completable timeout(long timeout, @NonNull @NonNull TimeUnit unit, @NonNull @NonNull Scheduler scheduler) Returns aCompletablethat runs thisCompletableand emits aTimeoutExceptionin case thisCompletabledoesn't complete within the given time while "waiting" on the specifiedScheduler.
- Scheduler:
timeoutsignals theTimeoutExceptionon theScheduleryou specify.
- Parameters:
timeout- the timeout valueunit- the unit oftimeoutscheduler- theSchedulerto use to wait for completion and signalTimeoutException- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifunitorschedulerisnull
-
timeout
@CheckReturnValue @NonNull @SchedulerSupport("custom") public final @NonNull Completable timeout(long timeout, @NonNull @NonNull TimeUnit unit, @NonNull @NonNull Scheduler scheduler, @NonNull @NonNull CompletableSource fallback) Returns aCompletablethat runs thisCompletableand switches to the otherCompletableSourcein case thisCompletabledoesn't complete within the given time while "waiting" on the specifiedScheduler.
- Scheduler:
timeoutsubscribes to the otherCompletableSourceon theScheduleryou specify.
- Parameters:
timeout- the timeout valueunit- the unit oftimeoutscheduler- theSchedulerto use to wait for completionfallback- the otherCompletableinstance to switch to in case of a timeout- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifunit,schedulerorfallbackisnull
-
to
@CheckReturnValue @SchedulerSupport("none") public final <R> R to(@NonNull @NonNull CompletableConverter<? extends R> converter) Calls the specifiedCompletableConverterfunction during assembly time and returns its resulting value.
This allows fluent conversion to any other type.
- Scheduler:
todoes not operate by default on a particularScheduler.
History: 2.1.7 - experimental
- Type Parameters:
R- the resulting object type- Parameters:
converter- theCompletableConverterthat receives the currentCompletableinstance and returns a value to be the result ofto()- Returns:
- the converted value
- Throws:
NullPointerException- ifconverterisnull- Since:
- 2.2
-
toFlowable
@CheckReturnValue @BackpressureSupport(FULL) @SchedulerSupport("none") @NonNull public final <@NonNull T> @NonNull Flowable<T> toFlowable()Returns aFlowablewhich when subscribed to subscribes to thisCompletableand relays the terminal events to the downstreamFlow.Subscriber.
- Backpressure:
- The returned
Flowablehonors the backpressure of the downstream consumer. - Scheduler:
toFlowabledoes not operate by default on a particularScheduler.
- Type Parameters:
T- the value type- Returns:
- the new
Flowableinstance
-
toFuture
Returns aFuturerepresenting the termination of the currentCompletablevia anullvalue.
Cancelling the
Futurewill cancel the subscription to the currentCompletable.- Scheduler:
toFuturedoes not operate by default on a particularScheduler.
- Returns:
- the new
Futureinstance - Since:
- 3.0.0
- See Also:
-
toMaybe
@CheckReturnValue @SchedulerSupport("none") @NonNull public final <@NonNull T> @NonNull Maybe<T> toMaybe()Converts thisCompletableinto aMaybe.
- Scheduler:
toMaybedoes not operate by default on a particularScheduler.
- Type Parameters:
T- the value type- Returns:
- the new
Maybeinstance
-
toObservable
@CheckReturnValue @SchedulerSupport("none") @NonNull public final <@NonNull T> @NonNull Observable<T> toObservable()Returns anObservablewhich when subscribed to subscribes to thisCompletableand relays the terminal events to the downstreamObserver.
- Scheduler:
toObservabledoes not operate by default on a particularScheduler.
- Type Parameters:
T- the value type- Returns:
- the new
Observablecreated
-
toSingle
@CheckReturnValue @NonNull @SchedulerSupport("none") public final <@NonNull T> @NonNull Single<T> toSingle(@NonNull @NonNull Supplier<? extends @NonNull T> completionValueSupplier) Converts thisCompletableinto aSinglewhich when thisCompletablecompletes normally, calls the givenSupplierand emits its returned value throughonSuccess.
- Scheduler:
toSingledoes not operate by default on a particularScheduler.
- Type Parameters:
T- the value type- Parameters:
completionValueSupplier- the value supplier called when thisCompletablecompletes normally- Returns:
- the new
Singleinstance - Throws:
NullPointerException- ifcompletionValueSupplierisnull
-
toSingleDefault
@CheckReturnValue @NonNull @SchedulerSupport("none") public final <@NonNull T> @NonNull Single<T> toSingleDefault(@NonNull T completionValue) Converts thisCompletableinto aSinglewhich when thisCompletablecompletes normally, emits the given value throughonSuccess.
- Scheduler:
toSingleDefaultdoes not operate by default on a particularScheduler.
- Type Parameters:
T- the value type- Parameters:
completionValue- the value to emit when thisCompletablecompletes normally- Returns:
- the new
Singleinstance - Throws:
NullPointerException- ifcompletionValueisnull
-
unsubscribeOn
@CheckReturnValue @NonNull @SchedulerSupport("custom") public final @NonNull Completable unsubscribeOn(@NonNull @NonNull Scheduler scheduler) Returns aCompletablewhich makes sure when an observer disposes the subscription, thedispose()method is called on the specifiedScheduler.
- Scheduler:
unsubscribeOncallsdispose()of the upstream on theScheduleryou specify.
- Parameters:
scheduler- the targetSchedulerwhere to execute the disposing- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifschedulerisnull
-
test
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull TestObserver<Void> test()Creates aTestObserverand subscribes it to thisCompletable.
- Scheduler:
testdoes not operate by default on a particularScheduler.
- Returns:
- the new
TestObserverinstance - Since:
- 2.0
-
test
@CheckReturnValue @SchedulerSupport("none") @NonNull public final @NonNull TestObserver<Void> test(boolean dispose) Creates aTestObserveroptionally in cancelled state, then subscribes it to thisCompletable.- Parameters:
dispose- iftrue, theTestObserverwill be cancelled before subscribing to thisCompletable.
- Scheduler:
testdoes not operate by default on a particularScheduler.
- Returns:
- the new
TestObserverinstance - Since:
- 2.0
-
fromCompletionStage
@CheckReturnValue @SchedulerSupport("none") @NonNull public static @NonNull Completable fromCompletionStage(@NonNull @NonNull CompletionStage<?> stage) Signals completion (or error) when theCompletionStageterminates.
Note that the operator takes an already instantiated, running or terminated
CompletionStage. If theCompletionStageis to be created per consumer upon subscription, usedefer(Supplier)aroundfromCompletionStage:Maybe.defer(() -> Completable.fromCompletionStage(createCompletionStage()));Canceling the flow can't cancel the execution of the
CompletionStagebecauseCompletionStageitself doesn't support cancellation. Instead, the operator detaches from theCompletionStage.- Scheduler:
fromCompletionStagedoes not operate by default on a particularScheduler.
- Parameters:
stage- theCompletionStageto convert to aCompletableand signalonCompleteoronErrorwhen theCompletionStageterminates normally or with a failure- Returns:
- the new
Completableinstance - Throws:
NullPointerException- ifstageisnull- Since:
- 3.0.0
-
toCompletionStage
@CheckReturnValue @SchedulerSupport("none") @NonNull public final <@Nullable T> @NonNull CompletionStage<T> toCompletionStage(@Nullable T defaultItem) Signals the given default item when the upstream completes or signals the upstream error via aCompletionStage.
The upstream can be canceled by converting the resulting
CompletionStageintoCompletableFutureviaCompletionStage.toCompletableFuture()and callingCompletableFuture.cancel(boolean)on it. The upstream will be also cancelled if the resultingCompletionStageis converted to and completed manually byCompletableFuture.complete(Object)orCompletableFuture.completeExceptionally(Throwable).CompletionStages don't have a notion of emptiness and allownulls, therefore, one can either use adefaultItemofnullor turn the flow into a sequence ofOptionals and default toOptional.empty():CompletionStage<Optional<T>> stage = source.map(Optional::of).toCompletionStage(Optional.empty());- Scheduler:
toCompletionStagedoes not operate by default on a particularScheduler.
- Type Parameters:
T- the type of the default item to signal upon completion- Parameters:
defaultItem- the item to signal upon completion- Returns:
- the new
CompletionStageinstance - Since:
- 3.0.0
-