SyncSubscription

public class SyncSubscription<T> : RealmCollectionValue where T : RealmCollectionValue

SyncSubscription represents a subscription to a set of objects in a synced Realm.

When partial sync is enabled for a synced Realm, the only objects that the server synchronizes to the client are those that match a sync subscription registered by that client. A subscription consists of of a query (represented by a Results) and an optional name.

Changes to the state of the subscription can be observed using SyncSubscription.observe(_:options:_:).

Subscriptions are created using Results.subscribe() or Results.subscribe(named:).

  • The name of the subscription.

    This will be nil if a name was not provided when the subscription was created.

    Declaration

    Swift

    public var name: String? { get }
  • The state of the subscription.

    Declaration

    Swift

    public var state: SyncSubscriptionState { get }
  • Declaration

    Swift

    public static func == (lhs: SyncSubscription, rhs: SyncSubscription) -> Bool
  • Observe the subscription for state changes.

    When the state of the subscription changes, block will be invoked and passed the new state.

    The token returned from this function does not hold a strong reference to this subscription object. This means that you must hold a reference to the subscription object itself along with the returned token in order to actually receive updates about the state.

    Declaration

    Swift

    public func observe(_ keyPath: KeyPath<SyncSubscription, SyncSubscriptionState>,
                        options: NSKeyValueObservingOptions = [],
                        _ block: @escaping (SyncSubscriptionState) -> Void) -> NotificationToken

    Parameters

    keyPath

    The path to observe. Must be \.state.

    options

    Options for the observation. Only NSKeyValueObservingOptions.initial option is is supported at this time.

    block

    The block to be called whenever a change occurs.

    Return Value

    A token which must be held for as long as you want updates to be delivered.

  • Remove this subscription

    Removing a subscription will delete all objects from the local Realm that were matched only by that subscription and not any remaining subscriptions. The deletion is performed by the server, and so has no immediate impact on the contents of the local Realm. If the device is currently offline, the removal will not be processed until the device returns online.

    Declaration

    Swift

    public func unsubscribe()