T
- the type of items emitted by the ConnectableObservable
public abstract class ConnectableObservable<T> extends Observable<T>
ConnectableObservable
resembles an ordinary Observable
, except that it does not begin
emitting items when it is subscribed to, but only when its connect(io.reactivex.rxjava3.functions.Consumer<? super io.reactivex.rxjava3.disposables.Disposable>)
method is called. In this way you
can wait for all intended Observer
s 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 Observer
s.
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 Observer
s.
Disposing the connection will reset the ConnectableFlowable
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 Observer
s are
still being subscribed to to this ConnectableObservable
to receive signals from the get go.
Constructor and Description |
---|
ConnectableObservable() |
Modifier and Type | Method and Description |
---|---|
Observable<T> |
autoConnect()
Returns an Observable that automatically connects (at most once) to this ConnectableObservable
when the first Observer subscribes.
|
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.
|
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.
|
Disposable |
connect()
Instructs the
ConnectableObservable to begin emitting the items from its underlying
Observable to its Observer s. |
abstract void |
connect(Consumer<? super Disposable> connection)
Instructs the
ConnectableObservable to begin emitting the items from its underlying
Observable to its Observer s. |
Observable<T> |
refCount()
Returns an
Observable that stays connected to this ConnectableObservable as long as there
is at least one subscription to this ConnectableObservable . |
Observable<T> |
refCount(int subscriberCount)
Connects to the upstream
ConnectableObservable if the number of subscribed
observers reaches the specified count and disconnect if all subscribers have unsubscribed. |
Observable<T> |
refCount(int subscriberCount,
long timeout,
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 subscribers have unsubscribed. |
Observable<T> |
refCount(int subscriberCount,
long timeout,
TimeUnit unit,
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 subscribers have unsubscribed. |
Observable<T> |
refCount(long timeout,
TimeUnit unit)
Connects to the upstream
ConnectableObservable if the number of subscribed
observers reaches 1 and disconnect after the specified
timeout if all subscribers have unsubscribed. |
Observable<T> |
refCount(long timeout,
TimeUnit unit,
Scheduler scheduler)
Connects to the upstream
ConnectableObservable if the number of subscribed
observers reaches 1 and disconnect after the specified
timeout if all subscribers have unsubscribed. |
abstract void |
reset()
Resets this ConnectableObservable into its fresh state if it has terminated
or has been disposed.
|
all, amb, ambArray, ambWith, any, blockingFirst, blockingFirst, blockingForEach, blockingIterable, blockingIterable, blockingLast, blockingLast, blockingLatest, blockingMostRecent, blockingNext, blockingSingle, blockingSingle, blockingSubscribe, blockingSubscribe, blockingSubscribe, blockingSubscribe, blockingSubscribe, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, bufferSize, cache, cacheWithInitialCapacity, cast, collect, collectInto, combineLatest, combineLatest, combineLatest, combineLatest, combineLatest, combineLatest, combineLatest, combineLatest, combineLatest, combineLatest, combineLatestArray, combineLatestArray, combineLatestDelayError, combineLatestDelayError, combineLatestDelayError, combineLatestDelayError, compose, concat, concat, concat, concat, concat, concat, concatArray, concatArrayDelayError, concatArrayEager, concatArrayEager, concatArrayEagerDelayError, concatArrayEagerDelayError, concatDelayError, concatDelayError, concatDelayError, concatEager, concatEager, concatEager, concatEager, concatMap, concatMap, concatMap, concatMapCompletable, concatMapCompletable, concatMapCompletableDelayError, concatMapCompletableDelayError, concatMapCompletableDelayError, concatMapDelayError, concatMapDelayError, concatMapDelayError, concatMapEager, concatMapEager, concatMapEagerDelayError, concatMapEagerDelayError, concatMapIterable, concatMapIterable, concatMapMaybe, concatMapMaybe, concatMapMaybeDelayError, concatMapMaybeDelayError, concatMapMaybeDelayError, concatMapSingle, concatMapSingle, concatMapSingleDelayError, concatMapSingleDelayError, concatMapSingleDelayError, concatWith, concatWith, concatWith, concatWith, contains, count, create, debounce, debounce, debounce, defaultIfEmpty, defer, delay, delay, delay, delay, delay, delay, delaySubscription, delaySubscription, delaySubscription, dematerialize, distinct, distinct, distinct, distinctUntilChanged, distinctUntilChanged, distinctUntilChanged, doAfterNext, doAfterTerminate, doFinally, doOnComplete, doOnDispose, doOnEach, doOnEach, doOnError, doOnLifecycle, doOnNext, doOnSubscribe, doOnTerminate, elementAt, elementAt, elementAtOrError, empty, error, error, filter, first, firstElement, firstOrError, flatMap, flatMap, flatMap, flatMap, flatMap, flatMap, flatMap, flatMap, flatMap, flatMap, flatMap, flatMap, flatMapCompletable, flatMapCompletable, flatMapIterable, flatMapIterable, flatMapMaybe, flatMapMaybe, flatMapSingle, flatMapSingle, forEach, forEachWhile, forEachWhile, forEachWhile, fromArray, fromCallable, fromFuture, fromFuture, fromFuture, fromFuture, fromIterable, fromPublisher, fromSupplier, generate, generate, generate, generate, generate, groupBy, groupBy, groupBy, groupBy, groupBy, groupJoin, hide, ignoreElements, interval, interval, interval, interval, intervalRange, intervalRange, isEmpty, join, just, just, just, just, just, just, just, just, just, just, last, lastElement, lastOrError, lift, map, materialize, merge, merge, merge, merge, merge, merge, merge, merge, mergeArray, mergeArray, mergeArrayDelayError, mergeArrayDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeWith, mergeWith, mergeWith, mergeWith, never, observeOn, observeOn, observeOn, ofType, onErrorResumeNext, onErrorResumeWith, onErrorReturn, onErrorReturnItem, onTerminateDetach, publish, publish, range, rangeLong, reduce, reduce, reduceWith, repeat, repeat, repeatUntil, repeatWhen, replay, replay, replay, replay, replay, replay, replay, replay, replay, replay, replay, replay, replay, replay, replay, replay, replay, replay, retry, retry, retry, retry, retry, retryUntil, retryWhen, safeSubscribe, sample, sample, sample, sample, sample, sample, scan, scan, scanWith, sequenceEqual, sequenceEqual, sequenceEqual, sequenceEqual, serialize, share, single, singleElement, singleOrError, skip, skip, skip, skipLast, skipLast, skipLast, skipLast, skipLast, skipLast, skipUntil, skipWhile, sorted, sorted, startWith, startWithArray, startWithItem, startWithIterable, subscribe, subscribe, subscribe, subscribe, subscribe, subscribeActual, subscribeOn, subscribeWith, switchIfEmpty, switchMap, switchMap, switchMapCompletable, switchMapCompletableDelayError, switchMapDelayError, switchMapDelayError, switchMapMaybe, switchMapMaybeDelayError, switchMapSingle, switchMapSingleDelayError, switchOnNext, switchOnNext, switchOnNextDelayError, switchOnNextDelayError, take, take, take, takeLast, takeLast, takeLast, takeLast, takeLast, takeLast, takeLast, takeLast, takeLast, takeUntil, takeUntil, takeWhile, test, test, throttleFirst, throttleFirst, throttleLast, throttleLast, throttleLatest, throttleLatest, throttleLatest, throttleLatest, throttleWithTimeout, throttleWithTimeout, timeInterval, timeInterval, timeInterval, timeInterval, timeout, timeout, timeout, timeout, timeout, timeout, timeout, timeout, timer, timer, timestamp, timestamp, timestamp, timestamp, to, toFlowable, toFuture, toList, toList, toList, toMap, toMap, toMap, toMultimap, toMultimap, toMultimap, toMultimap, toSortedList, toSortedList, toSortedList, toSortedList, unsafeCreate, unsubscribeOn, using, using, window, window, window, window, window, window, window, window, window, window, window, window, window, window, window, window, window, withLatestFrom, withLatestFrom, withLatestFrom, withLatestFrom, withLatestFrom, withLatestFrom, wrap, zip, zip, zip, zip, zip, zip, zip, zip, zip, zip, zip, zip, zipArray, zipWith, zipWith, zipWith, zipWith
public abstract void connect(@NonNull Consumer<? super Disposable> connection)
ConnectableObservable
to begin emitting the items from its underlying
Observable
to its Observer
s.connection
- the action that receives the connection subscription before the subscription to source happens
allowing the caller to synchronously disconnect a synchronous sourcepublic abstract void reset()
Calling this method on a fresh or active ConnectableObservable
has no effect.
public final Disposable connect()
ConnectableObservable
to begin emitting the items from its underlying
Observable
to its Observer
s.
To disconnect from a synchronous source, use the connect(Consumer)
method.
@NonNull @CheckReturnValue @SchedulerSupport(value="none") public Observable<T> refCount()
Observable
that stays connected to this ConnectableObservable
as long as there
is at least one subscription to this ConnectableObservable
.
refCount
overload does not operate on any particular Scheduler
.Observable
refCount(int)
,
refCount(long, TimeUnit)
,
refCount(int, long, TimeUnit)
@CheckReturnValue @SchedulerSupport(value="none") public final Observable<T> refCount(int subscriberCount)
ConnectableObservable
if the number of subscribed
observers reaches the specified count and disconnect if all subscribers have unsubscribed.
refCount
overload does not operate on any particular Scheduler
.History: 2.1.14 - experimental
subscriberCount
- the number of subscribers required to connect to the upstream@CheckReturnValue @SchedulerSupport(value="io.reactivex:computation") public final Observable<T> refCount(long timeout, TimeUnit unit)
ConnectableObservable
if the number of subscribed
observers reaches 1 and disconnect after the specified
timeout if all subscribers have unsubscribed.
refCount
overload operates on the computation
Scheduler
.History: 2.1.14 - experimental
timeout
- the time to wait before disconnecting after all subscribers unsubscribedunit
- the time unit of the timeoutrefCount(long, TimeUnit, Scheduler)
@CheckReturnValue @SchedulerSupport(value="custom") public final Observable<T> refCount(long timeout, TimeUnit unit, Scheduler scheduler)
ConnectableObservable
if the number of subscribed
observers reaches 1 and disconnect after the specified
timeout if all subscribers have unsubscribed.
refCount
overload operates on the specified Scheduler
.History: 2.1.14 - experimental
timeout
- the time to wait before disconnecting after all subscribers unsubscribedunit
- the time unit of the timeoutscheduler
- the target scheduler to wait on before disconnecting@CheckReturnValue @SchedulerSupport(value="io.reactivex:computation") public final Observable<T> refCount(int subscriberCount, long timeout, TimeUnit unit)
ConnectableObservable
if the number of subscribed
observers reaches the specified count and disconnect after the specified
timeout if all subscribers have unsubscribed.
refCount
overload operates on the computation
Scheduler
.History: 2.1.14 - experimental
subscriberCount
- the number of subscribers required to connect to the upstreamtimeout
- the time to wait before disconnecting after all subscribers unsubscribedunit
- the time unit of the timeoutrefCount(int, long, TimeUnit, Scheduler)
@CheckReturnValue @SchedulerSupport(value="custom") public final Observable<T> refCount(int subscriberCount, long timeout, TimeUnit unit, Scheduler scheduler)
ConnectableObservable
if the number of subscribed
observers reaches the specified count and disconnect after the specified
timeout if all subscribers have unsubscribed.
refCount
overload operates on the specified Scheduler
.History: 2.1.14 - experimental
subscriberCount
- the number of subscribers required to connect to the upstreamtimeout
- the time to wait before disconnecting after all subscribers unsubscribedunit
- the time unit of the timeoutscheduler
- the target scheduler to wait on before disconnecting@NonNull public Observable<T> autoConnect()
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 Observer
s have disposed their Disposable
s.
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.
@NonNull public Observable<T> autoConnect(int numberOfSubscribers)
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 Observer
s have disposed their Disposable
s.
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.
numberOfSubscribers
- the number of subscribers to await before calling connect
on the ConnectableObservable. A non-positive value indicates
an immediate connection.@NonNull public Observable<T> autoConnect(int numberOfSubscribers, @NonNull Consumer<? super Disposable> 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 Observer
s have disposed their Disposable
s.
numberOfSubscribers
- the number of subscribers to await before calling connect
on the ConnectableObservable. A non-positive value indicates
an immediate connection.connection
- the callback Consumer that will receive the Subscription representing the
established connection