Top Description Constructors Methods
io.reactivex.rxjava3.observables

public abstract Class ConnectableObservable<T>

extends Observable<T>
Class Inheritance
Known Direct Subclasses
io.reactivex.rxjava3.internal.operators.observable.ObservablePublish, io.reactivex.rxjava3.internal.operators.observable.ObservableReplay
Type Parameters
<T>
the type of items emitted by the ConnectableObservable
Imports
java.util.Objects, java.util.concurrent.TimeUnit, io.reactivex.rxjava3.annotations.*, io.reactivex.rxjava3.core.*, io.reactivex.rxjava3.disposables.Disposable, io.reactivex.rxjava3.functions.Consumer, io.reactivex.rxjava3.internal.functions.*, io.reactivex.rxjava3.internal.operators.observable.*, io.reactivex.rxjava3.internal.util.ConnectConsumer, io.reactivex.rxjava3.plugins.RxJavaPlugins, io.reactivex.rxjava3.schedulers.Schedulers

A ConnectableObservable resembles an ordinary Observable, except that it does not begin emitting items when it is subscribed to, but only when its connect method is called. In this way you can wait for all intended Observers to Observable#subscribe to the Observable before the Observable begins emitting items.

When the upstream terminates, the ConnectableObservable remains in this terminated state and, depending on the actual underlying implementation, relays cached events to late Observers. In order to reuse and restart this ConnectableObservable, the reset() method has to be called. When called, this ConnectableObservable will appear as fresh, unconnected source to new Observers. Disposing the connection will reset the ConnectableObservable to its fresh state and there is no need to call reset() in this case.

Note that although connect() and reset() are safe to call from multiple threads, it is recommended a dedicated thread or business logic manages the connection or resetting of a ConnectableObservable so that there is no unwanted signal loss due to early connect() or reset() calls while Observers are still being subscribed to to this ConnectableObservable to receive signals from the get go.

See Also
RxJava Wiki: Connectable Observable Operators

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public @NonNull Observable<T>

Returns:

a new Observable instance that automatically connects to this ConnectableObservable when the first Observer subscribes
autoConnect
()

Returns an Observable that automatically connects (at most once) to this ConnectableObservable when the first Observer subscribes.

public @NonNull Observable<T>

Returns:

a new Observable instance that automatically connects to this ConnectableObservable when the specified number of Observers subscribe to it
autoConnect
(int
the number of subscribers to await before calling connect on the ConnectableObservable. A non-positive value indicates an immediate connection.
numberOfObservers
)

Returns an Observable that automatically connects (at most once) to this ConnectableObservable when the specified number of Observers subscribe to it.

public @NonNull Observable<T>

Returns:

a new Observable instance that automatically connects to this ConnectableObservable when the specified number of Observers subscribe to it and calls the specified callback with the Disposable associated with the established connection
autoConnect
(int
the number of subscribers to await before calling connect on the ConnectableObservable. A non-positive value indicates an immediate connection.
numberOfObservers
,
@NonNull Consumer<? super Disposable>
the callback Consumer that will receive the Disposable representing the established connection
connection
)

Returns an Observable that automatically connects (at most once) to this ConnectableObservable when the specified number of Observers subscribe to it and calls the specified callback with the Disposable associated with the established connection.

public abstract void
connect(@NonNull Consumer<? super Disposable>
the action that receives the connection subscription before the subscription to source happens allowing the caller to synchronously disconnect a synchronous source
connection
)

Instructs the ConnectableObservable to begin emitting the items from its underlying Observable to its Observers.

public final @NonNull Disposable

Returns:

the Disposable representing the connection
connect
()

Instructs the ConnectableObservable to begin emitting the items from its underlying Observable to its Observers.

public @NonNull Observable<T>

Returns:

a new Observable instance
refCount
()

Returns an Observable that stays connected to this ConnectableObservable as long as there is at least one subscription to this ConnectableObservable.

public final @NonNull Observable<T>

Returns:

the new Observable instance
refCount
(int
the number of Observers required to connect to the upstream
observerCount
)

Connects to the upstream ConnectableObservable if the number of subscribed observers reaches the specified count and disconnect if all Observers have unsubscribed.

public final @NonNull Observable<T>

Returns:

the new Observable instance
refCount
(long
the time to wait before disconnecting after all Observers unsubscribed
timeout
,
@NonNull TimeUnit
the time unit of the timeout
unit
)

Connects to the upstream ConnectableObservable if the number of subscribed observers reaches 1 and disconnect after the specified timeout if all Observers have unsubscribed.

public final @NonNull Observable<T>

Returns:

the new Observable instance
refCount
(long
the time to wait before disconnecting after all Observers unsubscribed
timeout
,
@NonNull TimeUnit
the time unit of the timeout
unit
,
@NonNull Scheduler
the target scheduler to wait on before disconnecting
scheduler
)

Connects to the upstream ConnectableObservable if the number of subscribed observers reaches 1 and disconnect after the specified timeout if all Observers have unsubscribed.

public final @NonNull Observable<T>

Returns:

the new Observable instance
refCount
(int
the number of Observers required to connect to the upstream
observerCount
,
long
the time to wait before disconnecting after all Observers unsubscribed
timeout
,
@NonNull TimeUnit
the time unit of the timeout
unit
)

Connects to the upstream ConnectableObservable if the number of subscribed observers reaches the specified count and disconnect after the specified timeout if all Observers have unsubscribed.

public final @NonNull Observable<T>

Returns:

the new Observable instance
refCount
(int
the number of Observers required to connect to the upstream
observerCount
,
long
the time to wait before disconnecting after all Observers unsubscribed
timeout
,
@NonNull TimeUnit
the time unit of the timeout
unit
,
@NonNull Scheduler
the target scheduler to wait on before disconnecting
scheduler
)

Connects to the upstream ConnectableObservable if the number of subscribed observers reaches the specified count and disconnect after the specified timeout if all Observers have unsubscribed.

public abstract void
reset()

Resets this ConnectableObservable into its fresh state if it has terminated or has been disposed.

Inherited from io.reactivex.rxjava3.core.Observable:
allambambArrayambWithanyblockingFirstblockingFirstblockingForEachblockingForEachblockingIterableblockingIterableblockingLastblockingLastblockingLatestblockingMostRecentblockingNextblockingSingleblockingSingleblockingStreamblockingStreamblockingSubscribeblockingSubscribeblockingSubscribeblockingSubscribeblockingSubscribebufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferSizecachecacheWithInitialCapacitycastcollectcollectcollectIntocombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestArraycombineLatestArraycombineLatestArrayDelayErrorcombineLatestArrayDelayErrorcombineLatestDelayErrorcombineLatestDelayErrorcomposeconcatconcatconcatconcatconcatconcatconcatArrayconcatArrayDelayErrorconcatArrayEagerconcatArrayEagerconcatArrayEagerDelayErrorconcatArrayEagerDelayErrorconcatDelayErrorconcatDelayErrorconcatDelayErrorconcatEagerconcatEagerconcatEagerconcatEagerconcatEagerDelayErrorconcatEagerDelayErrorconcatEagerDelayErrorconcatEagerDelayErrorconcatMapconcatMapconcatMapconcatMapCompletableconcatMapCompletableconcatMapCompletableDelayErrorconcatMapCompletableDelayErrorconcatMapCompletableDelayErrorconcatMapDelayErrorconcatMapDelayErrorconcatMapDelayErrorconcatMapEagerconcatMapEagerconcatMapEagerDelayErrorconcatMapEagerDelayErrorconcatMapIterableconcatMapMaybeconcatMapMaybeconcatMapMaybeDelayErrorconcatMapMaybeDelayErrorconcatMapMaybeDelayErrorconcatMapSingleconcatMapSingleconcatMapSingleDelayErrorconcatMapSingleDelayErrorconcatMapSingleDelayErrorconcatMapStreamconcatWithconcatWithconcatWithconcatWithcontainscountcreatedebouncedebouncedebouncedebouncedefaultIfEmptydeferdelaydelaydelaydelaydelaydelaydelaySubscriptiondelaySubscriptiondelaySubscriptiondematerializedistinctdistinctdistinctdistinctUntilChangeddistinctUntilChangeddistinctUntilChangeddoAfterNextdoAfterTerminatedoFinallydoOnCompletedoOnDisposedoOnEachdoOnEachdoOnErrordoOnLifecycledoOnNextdoOnSubscribedoOnTerminateelementAtelementAtelementAtOrErroremptyerrorerrorfilterfirstfirstElementfirstOrErrorfirstOrErrorStagefirstStageflatMapflatMapflatMapflatMapflatMapflatMapflatMapflatMapflatMapflatMapflatMapflatMapflatMapCompletableflatMapCompletableflatMapIterableflatMapIterableflatMapMaybeflatMapMaybeflatMapSingleflatMapSingleflatMapStreamforEachforEachWhileforEachWhileforEachWhilefromActionfromArrayfromCallablefromCompletablefromCompletionStagefromFuturefromFuturefromIterablefromMaybefromOptionalfromPublisherfromRunnablefromSinglefromStreamfromSuppliergenerategenerategenerategenerategenerategroupBygroupBygroupBygroupBygroupBygroupJoinhideignoreElementsintervalintervalintervalintervalintervalRangeintervalRangeisEmptyjoinjustjustjustjustjustjustjustjustjustjustlastlastElementlastOrErrorlastOrErrorStagelastStageliftmapmapOptionalmaterializemergemergemergemergemergemergemergemergemergeArraymergeArraymergeArrayDelayErrormergeArrayDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeWithmergeWithmergeWithmergeWithneverobserveOnobserveOnobserveOnofTypeonErrorCompleteonErrorCompleteonErrorResumeNextonErrorResumeWithonErrorReturnonErrorReturnItemonTerminateDetachpublishpublishrangerangeLongreducereducereduceWithrepeatrepeatrepeatUntilrepeatWhenreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayretryretryretryretryretryretryUntilretryWhensafeSubscribesamplesamplesamplesamplesamplesamplesamplescanscanscanWithsequenceEqualsequenceEqualsequenceEqualsequenceEqualserializesharesinglesingleElementsingleOrErrorsingleOrErrorStagesingleStageskipskipskipskipLastskipLastskipLastskipLastskipLastskipLastskipUntilskipWhilesortedsortedstartWithstartWithstartWithstartWithstartWithArraystartWithItemstartWithIterablesubscribesubscribesubscribesubscribesubscribesubscribesubscribeActualsubscribeOnsubscribeWithswitchIfEmptyswitchMapswitchMapswitchMapCompletableswitchMapCompletableDelayErrorswitchMapDelayErrorswitchMapDelayErrorswitchMapMaybeswitchMapMaybeDelayErrorswitchMapSingleswitchMapSingleDelayErrorswitchOnNextswitchOnNextswitchOnNextDelayErrorswitchOnNextDelayErrortaketaketaketakeLasttakeLasttakeLasttakeLasttakeLasttakeLasttakeLasttakeLasttakeLasttakeUntiltakeUntiltakeWhiletesttestthrottleFirstthrottleFirstthrottleFirstthrottleLastthrottleLastthrottleLastthrottleLatestthrottleLatestthrottleLatestthrottleLatestthrottleLatestthrottleWithTimeoutthrottleWithTimeoutthrottleWithTimeouttimeIntervaltimeIntervaltimeIntervaltimeIntervaltimeouttimeouttimeouttimeouttimeouttimeouttimeouttimeouttimertimertimestamptimestamptimestamptimestamptotoFlowabletoFuturetoListtoListtoListtoMaptoMaptoMaptoMultimaptoMultimaptoMultimaptoMultimaptoSortedListtoSortedListtoSortedListtoSortedListunsafeCreateunsubscribeOnusingusingwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwithLatestFromwithLatestFromwithLatestFromwithLatestFromwithLatestFromwithLatestFromwrapzipzipzipzipzipzipzipzipzipzipzipzipzipArrayzipWithzipWithzipWithzipWith

Constructor Detail

ConnectableObservableback to summary
public ConnectableObservable()

Method Detail

autoConnectback to summary
public @NonNull Observable<T> autoConnect()

Returns an Observable that automatically connects (at most once) to this ConnectableObservable when the first Observer subscribes.

The connection happens after the first subscription and happens at most once during the lifetime of the returned Observable. If this ConnectableObservable terminates, the connection is never renewed, no matter how Observers come and go. Use refCount() to renew a connection or dispose an active connection when all Observers have disposed their Disposables.

This overload does not allow disconnecting the connection established via connect(Consumer). Use the autoConnect(int, Consumer) overload to gain access to the Disposable representing the only connection.

Scheduler:
autoConnect overload does not operate on any particular Scheduler.
Returns:@NonNull Observable<T>

a new Observable instance that automatically connects to this ConnectableObservable when the first Observer subscribes

Annotations
@NonNull
@CheckReturnValue
@SchedulerSupport:none
autoConnectback to summary
public @NonNull Observable<T> autoConnect(int numberOfObservers)

Returns an Observable that automatically connects (at most once) to this ConnectableObservable when the specified number of Observers subscribe to it.

The connection happens after the given number of subscriptions and happens at most once during the lifetime of the returned Observable. If this ConnectableObservable terminates, the connection is never renewed, no matter how Observers come and go. Use refCount() to renew a connection or dispose an active connection when all Observers have disposed their Disposables.

This overload does not allow disconnecting the connection established via connect(Consumer). Use the autoConnect(int, Consumer) overload to gain access to the Disposable representing the only connection.

Scheduler:
autoConnect overload does not operate on any particular Scheduler.
Parameters
numberOfObservers:int

the number of subscribers to await before calling connect on the ConnectableObservable. A non-positive value indicates an immediate connection.

Returns:@NonNull Observable<T>

a new Observable instance that automatically connects to this ConnectableObservable when the specified number of Observers subscribe to it

Annotations
@NonNull
@CheckReturnValue
@SchedulerSupport:none
autoConnectback to summary
public @NonNull Observable<T> autoConnect(int numberOfObservers, @NonNull Consumer<? super Disposable> connection)

Returns an Observable that automatically connects (at most once) to this ConnectableObservable when the specified number of Observers subscribe to it and calls the specified callback with the Disposable associated with the established connection.

The connection happens after the given number of subscriptions and happens at most once during the lifetime of the returned Observable. If this ConnectableObservable terminates, the connection is never renewed, no matter how Observers come and go. Use refCount() to renew a connection or dispose an active connection when all Observers have disposed their Disposables.

Scheduler:
autoConnect overload does not operate on any particular Scheduler.
Parameters
numberOfObservers:int

the number of subscribers to await before calling connect on the ConnectableObservable. A non-positive value indicates an immediate connection.

connection:@NonNull Consumer<? super Disposable>

the callback Consumer that will receive the Disposable representing the established connection

Returns:@NonNull Observable<T>

a new Observable instance that automatically connects to this ConnectableObservable when the specified number of Observers subscribe to it and calls the specified callback with the Disposable associated with the established connection

Annotations
@NonNull
@CheckReturnValue
@SchedulerSupport:none
Exceptions
NullPointerException:
if connection is null
connectback to summary
public abstract void connect(@NonNull Consumer<? super Disposable> connection)

Instructs the ConnectableObservable to begin emitting the items from its underlying Observable to its Observers.

Scheduler:
The behavior is determined by the implementor of this abstract class.
Parameters
connection:@NonNull Consumer<? super Disposable>

the action that receives the connection subscription before the subscription to source happens allowing the caller to synchronously disconnect a synchronous source

Annotations
@SchedulerSupport:none
Exceptions
NullPointerException:
if connection is null
See Also
ReactiveX documentation: Connect
connectback to summary
public final @NonNull Disposable connect()

Instructs the ConnectableObservable to begin emitting the items from its underlying Observable to its Observers.

To disconnect from a synchronous source, use the connect(Consumer) method.

Scheduler:
The behavior is determined by the implementor of this abstract class.
Returns:@NonNull Disposable

the Disposable representing the connection

Annotations
@NonNull
@SchedulerSupport:none
See Also
ReactiveX documentation: Connect
refCountback to summary
public @NonNull Observable<T> refCount()

Returns an Observable that stays connected to this ConnectableObservable as long as there is at least one subscription to this ConnectableObservable.

Scheduler:
This refCount overload does not operate on any particular Scheduler.
Returns:@NonNull Observable<T>

a new Observable instance

Annotations
@NonNull
@CheckReturnValue
@SchedulerSupport:none
See Also
ReactiveX documentation: RefCount, refCount(int), refCount(long, TimeUnit), refCount(int, long, TimeUnit)
refCountback to summary
public final @NonNull Observable<T> refCount(int observerCount)

Connects to the upstream ConnectableObservable if the number of subscribed observers reaches the specified count and disconnect if all Observers have unsubscribed.

Scheduler:
This refCount overload does not operate on any particular Scheduler.

History: 2.1.14 - experimental

Parameters
observerCount:int

the number of Observers required to connect to the upstream

Returns:@NonNull Observable<T>

the new Observable instance

Annotations
@CheckReturnValue
@SchedulerSupport:none
@NonNull
Exceptions
IllegalArgumentException:
if observerCount is non-positive
Since
2.2
refCountback to summary
public final @NonNull Observable<T> refCount(long timeout, @NonNull TimeUnit unit)

Connects to the upstream ConnectableObservable if the number of subscribed observers reaches 1 and disconnect after the specified timeout if all Observers have unsubscribed.

Scheduler:
This refCount overload operates on the computation Scheduler.

History: 2.1.14 - experimental

Parameters
timeout:long

the time to wait before disconnecting after all Observers unsubscribed

unit:@NonNull TimeUnit

the time unit of the timeout

Returns:@NonNull Observable<T>

the new Observable instance

Annotations
@CheckReturnValue
@SchedulerSupport:io.reactivex:computation
@NonNull
Exceptions
NullPointerException:
if unit is null
Since
2.2
See Also
refCount(long, TimeUnit, Scheduler)
refCountback to summary
public final @NonNull Observable<T> refCount(long timeout, @NonNull TimeUnit unit, @NonNull Scheduler scheduler)

Connects to the upstream ConnectableObservable if the number of subscribed observers reaches 1 and disconnect after the specified timeout if all Observers have unsubscribed.

Scheduler:
This refCount overload operates on the specified Scheduler.

History: 2.1.14 - experimental

Parameters
timeout:long

the time to wait before disconnecting after all Observers unsubscribed

unit:@NonNull TimeUnit

the time unit of the timeout

scheduler:@NonNull Scheduler

the target scheduler to wait on before disconnecting

Returns:@NonNull Observable<T>

the new Observable instance

Annotations
@CheckReturnValue
@SchedulerSupport:custom
@NonNull
Exceptions
NullPointerException:
if unit or scheduler is null
Since
2.2
refCountback to summary
public final @NonNull Observable<T> refCount(int observerCount, long timeout, @NonNull TimeUnit unit)

Connects to the upstream ConnectableObservable if the number of subscribed observers reaches the specified count and disconnect after the specified timeout if all Observers have unsubscribed.

Scheduler:
This refCount overload operates on the computation Scheduler.

History: 2.1.14 - experimental

Parameters
observerCount:int

the number of Observers required to connect to the upstream

timeout:long

the time to wait before disconnecting after all Observers unsubscribed

unit:@NonNull TimeUnit

the time unit of the timeout

Returns:@NonNull Observable<T>

the new Observable instance

Annotations
@CheckReturnValue
@SchedulerSupport:io.reactivex:computation
@NonNull
Exceptions
NullPointerException:
if unit or scheduler is null
IllegalArgumentException:
if observerCount is non-positive
Since
2.2
See Also
refCount(int, long, TimeUnit, Scheduler)
refCountback to summary
public final @NonNull Observable<T> refCount(int observerCount, long timeout, @NonNull TimeUnit unit, @NonNull Scheduler scheduler)

Connects to the upstream ConnectableObservable if the number of subscribed observers reaches the specified count and disconnect after the specified timeout if all Observers have unsubscribed.

Scheduler:
This refCount overload operates on the specified Scheduler.

History: 2.1.14 - experimental

Parameters
observerCount:int

the number of Observers required to connect to the upstream

timeout:long

the time to wait before disconnecting after all Observers unsubscribed

unit:@NonNull TimeUnit

the time unit of the timeout

scheduler:@NonNull Scheduler

the target scheduler to wait on before disconnecting

Returns:@NonNull Observable<T>

the new Observable instance

Annotations
@CheckReturnValue
@SchedulerSupport:custom
@NonNull
Exceptions
NullPointerException:
if unit or scheduler is null
IllegalArgumentException:
if observerCount is non-positive
Since
2.2
resetback to summary
public abstract void reset()

Resets this ConnectableObservable into its fresh state if it has terminated or has been disposed.

Calling this method on a fresh or active ConnectableObservable has no effect.

Scheduler:
The behavior is determined by the implementor of this abstract class.
Annotations
@SchedulerSupport:none
Since
3.0.0