Class SubscriptionBuilder<T>

java.lang.Object
io.objectbox.reactive.SubscriptionBuilder<T>
Type Parameters:
T - The data type the DataObserver subscribes to.

public class SubscriptionBuilder<T> extends Object
Builds a 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:

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 Details

  • Method Details

    • 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

      public SubscriptionBuilder<T> onlyChanges()
      Upon subscribing do not deliver the latest data, only once there are changes.
      See Also:
    • 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 given ErrorObserver is notified when the DataTransformer (transform(DataTransformer)) or DataObserver (observer(DataObserver)) threw an exception.
    • on

      public SubscriptionBuilder<T> on(Scheduler scheduler)
      Changes the thread in which the DataObserver (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)
      Completes building the subscription by setting a DataObserver that receives the data.

      By default, requests the latest data to be delivered immediately and on any future updates. To change this call single() or onlyChanges() before.

      By default, DataObserver.onData(Object) is called from an internal background thread. Change this by setting a custom scheduler using on(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

      public SubscriptionBuilder<T> dataSubscriptionList(DataSubscriptionList dataSubscriptionList)