Class SubscriptionBuilder<T>
- Type Parameters:
T
- The data type theDataObserver
subscribes to.
DataSubscription
for a DataObserver
passed via observer(DataObserver)
.
Note that the call to observer(DataObserver)
is mandatory to create the subscription -
if you forget it, nothing will happen.
When subscribing to a data source such as Query
, this builder allows to configure:
- weakly referenced observer via
weak()
- a data transform operation via
transform(DataTransformer)
- error handlers via
onError(ErrorObserver)
- calling the observer using a custom
Scheduler
(e.g. Android main thread) viaon(Scheduler)
Note: the order of methods called in this do not matter. Unlike Rx's Observeable, this builder just collects all info. For example, on(scheduler).transform(transformer) is the same as transform(transformer).on(scheduler).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondataSubscriptionList
(DataSubscriptionList dataSubscriptionList) observer
(DataObserver<T> observer) Completes building the subscription by setting aDataObserver
that receives the data.Changes the thread in which theDataObserver
(and potentially @ErrorObserver
) is called.onError
(ErrorObserver errorObserver) The givenErrorObserver
is notified when theDataTransformer
(transform(DataTransformer)
) orDataObserver
(observer(DataObserver)
) threw an exception.Upon subscribing do not deliver the latest data, only once there are changes.single()
Only deliver the latest data once, do not subscribe for data changes.<TO> SubscriptionBuilder<TO>
transform
(DataTransformer<T, TO> transformer) Transforms the original data from the publisher to some other type.weak()
Uses a weak reference for the observer.
-
Constructor Details
-
SubscriptionBuilder
-
-
Method Details
-
weak
Uses a weak reference for the observer. It is still advised to remove observers explicitly if possible: relying on the garbage collection may cause non-deterministic timing. Until the weak reference is actually cleared by GC, it may still receive notifications. -
single
Only deliver the latest data once, do not subscribe for data changes.- See Also:
-
onlyChanges
Upon subscribing do not deliver the latest data, only once there are changes.- See Also:
-
transform
Transforms the original data from the publisher to some other type. All transformations run sequentially in an asynchronous thread owned by the publisher.This is similar to the map operator of Rx and Kotlin.
- Type Parameters:
TO
- The type data is transformed to.
-
onError
The givenErrorObserver
is notified when theDataTransformer
(transform(DataTransformer)
) orDataObserver
(observer(DataObserver)
) threw an exception. -
on
Changes the thread in which theDataObserver
(and potentially @ErrorObserver
) is called.In the Android package, there is a class AndroidScheduler with a MAIN_THREAD Scheduler implementation. Using MAIN_THREAD, observers will be called in Android's main thread, which is required for UI updates.
-
observer
Completes building the subscription by setting aDataObserver
that receives the data.By default, requests the latest data to be delivered immediately and on any future updates. To change this call
single()
oronlyChanges()
before.By default,
DataObserver.onData(Object)
is called from an internal background thread. Change this by setting a custom scheduler usingon(Scheduler)
. It may also get called for multiple observers at the same time. The order in which observers are called is the same as the subscription order, although this may change in the future.Typically, keep a reference to the returned
DataSubscription
to avoid it getting garbage collected, to keep receiving new data.Call
DataSubscription.cancel()
once the observer should no longer receive data. -
dataSubscriptionList
-