SyncSubscription

public struct SyncSubscription : 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 }
  • The raw query which this subscription is running on the server.

    This string is a serialized representation of the Results which the subscription was created from. This representation does not use NSPredicate syntax, and is not guaranteed to remain consistent between versions of Realm. Any use of this other than manual inspection when debugging is likely to be incorrect.

    This is nil while the subscription is in the Creating state.

    Declaration

    Swift

    public var query: String? { get }
  • When this subscription was first created.

    This value will be nil for subscriptions created with older versions of Realm which did not store the creation date. Newly created subscriptions should always have a non-nil creation date.

    Declaration

    Swift

    public var createdAt: Date? { get }
  • When this subscription was last updated.

    This value will be nil for subscriptions created with older versions of Realm which did not store the update date. Newly created subscriptions should always have a non-nil update date.

    The update date is the time when the subscription was last updated by a call to Results.subscribe(), and not when the set of objects which match the subscription last changed.

    Declaration

    Swift

    public var updatedAt: Date? { get }
  • When this subscription will be automatically removed.

    If the timeToLive parameter is set when creating a sync subscription, the subscription will be automatically removed the first time that any subscription is created, modified, or deleted after that time has elapsed.

    This property will be nil if the timeToLive option was not enabled.

    Declaration

    Swift

    public var expiresAt: Date? { get }
  • How long this subscription will persist after last being updated.

    If the timeToLive parameter is set when creating a sync subscription, the subscription will be automatically removed the first time that any subscription is created, modified, or deleted after that time has elapsed.

    This property will be nil if the timeToLive option was not enabled.

    Declaration

    Swift

    public var timeToLive: TimeInterval? { 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()