Top Description Constructors Methods
io.reactivex.observables

public abstract Class ConnectableObservable<T>

extends Observable<T>
Class Inheritance
Known Direct Subclasses
io.reactivex.internal.operators.observable.ObservableReplay, io.reactivex.internal.operators.observable.ObservableReplay.Replay, io.reactivex.internal.operators.observable.ObservablePublish, io.reactivex.internal.operators.observable.ObservablePublishAlt
Type Parameters
<T>
the type of items emitted by the ConnectableObservable
Imports
java.util.concurrent.TimeUnit, io.reactivex.*, io.reactivex.annotations.*, io.reactivex.disposables.Disposable, io.reactivex.functions.Consumer, io.reactivex.internal.functions.*, io.reactivex.internal.operators.observable.*, io.reactivex.internal.util.ConnectConsumer, io.reactivex.plugins.RxJavaPlugins, io.reactivex.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.

See Also
RxJava Wiki: Connectable Observable Operators

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Observable<T>

Returns:

an Observable 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 Observable<T>

Returns:

an Observable that automatically connects to this ConnectableObservable when the specified number of Subscribers 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.
numberOfSubscribers
)

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

public Observable<T>

Returns:

an Observable that automatically connects to this ConnectableObservable when the specified number of Subscribers subscribe to it and calls the specified callback with the Subscription 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.
numberOfSubscribers
,
Consumer<? super Disposable>
the callback Consumer that will receive the Subscription representing the established connection
connection
)

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

public abstract void
connect(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 Disposable

Returns:

the subscription representing the connection
connect
()

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

private ConnectableObservable<T>

Returns:

the ConnectableObservable to work with
onRefCount
()

Apply a workaround for a race condition with the regular publish().refCount() so that racing observers and refCount won't hang.

public Observable<T>

Returns:

an Observable
refCount
()

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

public final Observable<T>

Returns:

the new Observable instance
refCount
(int
the number of subscribers required to connect to the upstream
subscriberCount
)

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

public final Observable<T>

Returns:

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

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

public final Observable<T>

Returns:

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

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

public final Observable<T>

Returns:

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

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

public final Observable<T>

Returns:

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

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

Inherited from io.reactivex.Observable:
allambambArrayambWithanyasblockingFirstblockingFirstblockingForEachblockingIterableblockingIterableblockingLastblockingLastblockingLatestblockingMostRecentblockingNextblockingSingleblockingSingleblockingSubscribeblockingSubscribeblockingSubscribeblockingSubscribeblockingSubscribebufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferbufferSizecachecacheWithInitialCapacitycastcollectcollectIntocombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestcombineLatestDelayErrorcombineLatestDelayErrorcombineLatestDelayErrorcombineLatestDelayErrorcombineLatestDelayErrorcomposeconcatconcatconcatconcatconcatconcatconcatArrayconcatArrayDelayErrorconcatArrayEagerconcatArrayEagerconcatArrayEagerDelayErrorconcatArrayEagerDelayErrorconcatDelayErrorconcatDelayErrorconcatDelayErrorconcatEagerconcatEagerconcatEagerconcatEagerconcatMapconcatMapconcatMapCompletableconcatMapCompletableconcatMapCompletableDelayErrorconcatMapCompletableDelayErrorconcatMapCompletableDelayErrorconcatMapDelayErrorconcatMapDelayErrorconcatMapEagerconcatMapEagerconcatMapEagerDelayErrorconcatMapEagerDelayErrorconcatMapIterableconcatMapIterableconcatMapMaybeconcatMapMaybeconcatMapMaybeDelayErrorconcatMapMaybeDelayErrorconcatMapMaybeDelayErrorconcatMapSingleconcatMapSingleconcatMapSingleDelayErrorconcatMapSingleDelayErrorconcatMapSingleDelayErrorconcatWithconcatWithconcatWithconcatWithcontainscountcreatedebouncedebouncedebouncedefaultIfEmptydeferdelaydelaydelaydelaydelaydelaydelaySubscriptiondelaySubscriptiondelaySubscriptiondematerializedematerializedistinctdistinctdistinctdistinctUntilChangeddistinctUntilChangeddistinctUntilChangeddoAfterNextdoAfterTerminatedoFinallydoOnCompletedoOnDisposedoOnEachdoOnEachdoOnErrordoOnLifecycledoOnNextdoOnSubscribedoOnTerminateelementAtelementAtelementAtOrErroremptyerrorerrorfilterfirstfirstElementfirstOrErrorflatMapflatMapflatMapflatMapflatMapflatMapflatMapflatMapflatMapflatMapflatMapflatMapflatMapCompletableflatMapCompletableflatMapIterableflatMapIterableflatMapMaybeflatMapMaybeflatMapSingleflatMapSingleforEachforEachWhileforEachWhileforEachWhilefromArrayfromCallablefromFuturefromFuturefromFuturefromFuturefromIterablefromPublishergenerategenerategenerategenerategenerategroupBygroupBygroupBygroupBygroupBygroupJoinhideignoreElementsintervalintervalintervalintervalintervalRangeintervalRangeisEmptyjoinjustjustjustjustjustjustjustjustjustjustlastlastElementlastOrErrorliftmapmaterializemergemergemergemergemergemergemergemergemergeArraymergeArraymergeArrayDelayErrormergeArrayDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeDelayErrormergeWithmergeWithmergeWithmergeWithneverobserveOnobserveOnobserveOnofTypeonErrorResumeNextonErrorResumeNextonErrorReturnonErrorReturnItemonExceptionResumeNextonTerminateDetachpublishpublishrangerangeLongreducereducereduceWithrepeatrepeatrepeatUntilrepeatWhenreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayreplayretryretryretryretryretryretryUntilretryWhensafeSubscribesamplesamplesamplesamplesamplesamplescanscanscanWithsequenceEqualsequenceEqualsequenceEqualsequenceEqualserializesharesinglesingleElementsingleOrErrorskipskipskipskipLastskipLastskipLastskipLastskipLastskipLastskipUntilskipWhilesortedsortedstartWithstartWithstartWithstartWithArraysubscribesubscribesubscribesubscribesubscribesubscribesubscribeActualsubscribeOnsubscribeWithswitchIfEmptyswitchMapswitchMapswitchMapCompletableswitchMapCompletableDelayErrorswitchMapDelayErrorswitchMapDelayErrorswitchMapMaybeswitchMapMaybeDelayErrorswitchMapSingleswitchMapSingleDelayErrorswitchOnNextswitchOnNextswitchOnNextDelayErrorswitchOnNextDelayErrortaketaketaketakeLasttakeLasttakeLasttakeLasttakeLasttakeLasttakeLasttakeLasttakeLasttakeUntiltakeUntiltakeWhiletesttestthrottleFirstthrottleFirstthrottleLastthrottleLastthrottleLatestthrottleLatestthrottleLatestthrottleLatestthrottleWithTimeoutthrottleWithTimeouttimeIntervaltimeIntervaltimeIntervaltimeIntervaltimeouttimeouttimeouttimeouttimeouttimeouttimeouttimeouttimertimertimestamptimestamptimestamptimestamptotoFlowabletoFuturetoListtoListtoListtoMaptoMaptoMaptoMultimaptoMultimaptoMultimaptoMultimaptoSortedListtoSortedListtoSortedListtoSortedListunsafeCreateunsubscribeOnusingusingwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwindowwithLatestFromwithLatestFromwithLatestFromwithLatestFromwithLatestFromwithLatestFromwrapzipzipzipzipzipzipzipzipzipzipzipzipzipArrayzipIterablezipWithzipWithzipWithzipWith

Constructor Detail

ConnectableObservableback to summary
public ConnectableObservable()

Method Detail

autoConnectback to summary
public 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.

Returns:Observable<T>

an Observable that automatically connects to this ConnectableObservable when the first Observer subscribes

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

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.

Parameters
numberOfSubscribers:int

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

Returns:Observable<T>

an Observable that automatically connects to this ConnectableObservable when the specified number of Subscribers subscribe to it

Annotations
@NonNull
autoConnectback to summary
public Observable<T> autoConnect(int numberOfSubscribers, Consumer<? super Disposable> connection)

Returns an Observable that automatically connects (at most once) to this ConnectableObservable when the specified number of Subscribers subscribe to it and calls the specified callback with the Subscription 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.

Parameters
numberOfSubscribers:int

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

connection:Consumer<? super Disposable>

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

Returns:Observable<T>

an Observable that automatically connects to this ConnectableObservable when the specified number of Subscribers subscribe to it and calls the specified callback with the Subscription associated with the established connection

Annotations
@NonNull
connectback to summary
public abstract void connect(Consumer<? super Disposable> connection)

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

Parameters
connection: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

See Also
ReactiveX documentation: Connect
connectback to summary
public final 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.

Returns:Disposable

the subscription representing the connection

See Also
ReactiveX documentation: Connect
onRefCountback to summary
private ConnectableObservable<T> onRefCount()

Apply a workaround for a race condition with the regular publish().refCount() so that racing observers and refCount won't hang.

Returns:ConnectableObservable<T>

the ConnectableObservable to work with

Annotations
@SuppressWarnings:unchecked
Since
2.2.10
refCountback to summary
public 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:Observable<T>

an Observable

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

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

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

History: 2.1.14 - experimental

Parameters
subscriberCount:int

the number of subscribers required to connect to the upstream

Returns:Observable<T>

the new Observable instance

Annotations
@CheckReturnValue
@SchedulerSupport:none
Since
2.2
refCountback to summary
public final Observable<T> refCount(long timeout, TimeUnit unit)

Connects to the upstream ConnectableObservable if the number of subscribed subscriber reaches 1 and disconnect after the specified timeout if all subscribers 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 subscribers unsubscribed

unit:TimeUnit

the time unit of the timeout

Returns:Observable<T>

the new Observable instance

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

Connects to the upstream ConnectableObservable if the number of subscribed subscriber reaches 1 and disconnect after the specified timeout if all subscribers 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 subscribers unsubscribed

unit:TimeUnit

the time unit of the timeout

scheduler:Scheduler

the target scheduler to wait on before disconnecting

Returns:Observable<T>

the new Observable instance

Annotations
@CheckReturnValue
@SchedulerSupport:custom
Since
2.2
refCountback to summary
public final Observable<T> refCount(int subscriberCount, long timeout, TimeUnit unit)

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

Scheduler:
This refCount overload operates on the computation Scheduler.

History: 2.1.14 - experimental

Parameters
subscriberCount:int

the number of subscribers required to connect to the upstream

timeout:long

the time to wait before disconnecting after all subscribers unsubscribed

unit:TimeUnit

the time unit of the timeout

Returns:Observable<T>

the new Observable instance

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

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

Scheduler:
This refCount overload operates on the specified Scheduler.

History: 2.1.14 - experimental

Parameters
subscriberCount:int

the number of subscribers required to connect to the upstream

timeout:long

the time to wait before disconnecting after all subscribers unsubscribed

unit:TimeUnit

the time unit of the timeout

scheduler:Scheduler

the target scheduler to wait on before disconnecting

Returns:Observable<T>

the new Observable instance

Annotations
@CheckReturnValue
@SchedulerSupport:custom
Since
2.2