Class SubscriptionBuilder<T>
- java.lang.Object
-
- io.objectbox.reactive.SubscriptionBuilder<T>
-
- Type Parameters:
T
- The data type theDataObserver
subscribes to.
public class SubscriptionBuilder<T> extends java.lang.Object
Builds aDataSubscription
for aDataObserver
passed viaobserver(DataObserver)
. Note that the call toobserver(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 Constructor Description SubscriptionBuilder(DataPublisher<T> publisher, java.lang.Object param)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description SubscriptionBuilder<T>
dataSubscriptionList(DataSubscriptionList dataSubscriptionList)
DataSubscription
observer(DataObserver<T> observer)
Sets the observer for this subscription and requests the latest data to be delivered immediately.SubscriptionBuilder<T>
on(Scheduler scheduler)
Changes the thread in which theDataObserver
(and potentially @ErrorObserver
) is called.SubscriptionBuilder<T>
onError(ErrorObserver errorObserver)
The givenErrorObserver
is notified when theDataTransformer
(transform(DataTransformer)
) orDataObserver
(observer(DataObserver)
) threw an exception.SubscriptionBuilder<T>
onlyChanges()
Upon subscribing do not deliver the latest data, only once there are changes.SubscriptionBuilder<T>
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.SubscriptionBuilder<T>
weak()
Uses a weak reference for the observer.
-
-
-
Constructor Detail
-
SubscriptionBuilder
@Internal public SubscriptionBuilder(DataPublisher<T> publisher, @Nullable java.lang.Object param)
-
-
Method Detail
-
weak
public SubscriptionBuilder<T> 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
public SubscriptionBuilder<T> single()
Only deliver the latest data once, do not subscribe for data changes.- See Also:
onlyChanges()
-
onlyChanges
public SubscriptionBuilder<T> onlyChanges()
Upon subscribing do not deliver the latest data, only once there are changes.- See Also:
single()
-
transform
public <TO> SubscriptionBuilder<TO> transform(DataTransformer<T,TO> transformer)
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
public SubscriptionBuilder<T> onError(ErrorObserver errorObserver)
The givenErrorObserver
is notified when theDataTransformer
(transform(DataTransformer)
) orDataObserver
(observer(DataObserver)
) threw an exception.
-
on
public SubscriptionBuilder<T> on(Scheduler scheduler)
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
public DataSubscription observer(DataObserver<T> observer)
Sets the observer for this subscription and requests the latest data to be delivered immediately. Subscribes to receive data updates. This can be changed by usingsingle()
oronlyChanges()
.Results are delivered on a background thread owned by the internal data publisher, unless a scheduler was set using
on(Scheduler)
.The returned
DataSubscription
must be canceled once the observer should no longer receive data.
-
dataSubscriptionList
public SubscriptionBuilder<T> dataSubscriptionList(DataSubscriptionList dataSubscriptionList)
-
-