Class SubscriptionBuilder<T>

  • Type Parameters:
    T - The data type the DataObserver subscribes to.

    public class SubscriptionBuilder<T>
    extends java.lang.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 Detail

      • SubscriptionBuilder

        public SubscriptionBuilder​(DataPublisher<T> publisher,
                                   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.
      • 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.
      • 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)
        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 using single() or onlyChanges().

        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.