Realm.Sync.Subscription

An immutable object encapsulating a snapshot of the state of a query-based sync subscriptions.

error

Returns the error message if the server encountered an error when evaluating the query covered by this subscription. undefined is returned if state != Realm.Sync.SubscriptionState.Error.

Type:
string
state

Gets the current state of the subscription. Can be either:

  • Realm.Sync.SubscriptionState.Error: An error occurred while creating or processing the query-based sync subscription.
  • Realm.Sync.SubscriptionState.Creating: The subscription is being created.
  • Realm.Sync.SubscriptionState.Pending: The subscription was created, but has not yet been processed by the sync server.
  • Realm.Sync.SubscriptionState.Complete: The subscription has been processed by the sync server and data is being synced to the device.
  • Realm.Sync.SubscriptionState.Invalidated: The subscription has been removed.
Type:
number
addListener(callback)

Adds a listener callback which will be called when the state of the subscription changes.

Parameters:
  • callback
    • Type: function
    • A function to be called when changes to the subscription occur.

Throws:
  • Error
    • If callback is not a function.

Example:
let subscription = results.subscribe();
subscription.addListener((subscription, state) => {
    switch (state) {
    case Realm.Sync.SubscriptionState.Complete:
        // results is ready to be consumed
        break;
    case Realm.Sync.SubscriptionState.Error:
        console.log('An error occurred: ', subscription.error);
        break;
    }
}
removeAllListeners()

Remove all listeners from the subscription instance.

removeListener(callback)

Remove the listener callback from the subscription instance.

Parameters:
  • callback
    • Type: function
    • Callback function that was previously added as a listener through the addListener method.

Throws:
  • Error
    • If callback is not a function.

unsubscribe()

Unsubscribe a query-based synced Realm.Results. The state will change to Realm.Sync.SubscriptionState.Invalidated. The Realm.Results will not produce any meaningful values. Moreover, any objects matching the query will be removed if they are not matched by any other query. The object removal is done asynchronously.