Realm.Sync.Subscription

An object encapsulating query-based sync subscriptions.

error

Gets the error message. undefined if no 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.