subscribe

suspend fun <T : RealmObject> RealmQuery<T>.subscribe(name: String, updateExisting: Boolean = false, mode: WaitForSync = WaitForSync.FIRST_TIME, timeout: Duration = Duration.INFINITE): RealmResults<T>

Automatically create a named Subscription from a query in the background and return the result of running the same query against the local Realm file.

This is a more streamlined alternative to doing something like this:

fun suspend getData(realm: Realm): RealmResults<Person> {
realm.subscriptions.update { bgRealm ->
add("myquery", bgRealm.query<Person>())
}
realm.subscriptions.waitForSynchronization()
return realm.query<Person>().find()
}

It is possible to define whether or not to wait for the server to send all data before running the local query. This is relevant as there might be delay from creating a subscription to the data being available on the device due to either latency or because a large dataset needs to be downloaded.

The default behaviour is that the first time subscribe is called, the query result will not be returned until data has been downloaded from the server. On subsequent calls to subscribe for the same query, the query will run immediately on the local database while any updates are downloaded in the background.

Return

The result of running the query against the local Realm file. The results returned will depend on which mode was used.

Parameters

name

name of the subscription. This can be used to identify it later in the SubscriptionSet.

mode

mode used to resolve the subscription. See WaitForSync for more details.

timeout

How long to wait for the server to return the objects defined by the subscription. This is only relevant for WaitForSync.ALWAYS and WaitForSync.FIRST_TIME.

Throws

TimeoutCancellationException

if the specified timeout was hit before a query result could be returned.

if this method is called on a Realm that isn't using Flexible Sync.

if the server did not accept the set of queries. The exact reason is found in the exception message.


suspend fun <T : RealmObject> RealmQuery<T>.subscribe(mode: WaitForSync = WaitForSync.FIRST_TIME, timeout: Duration = Duration.INFINITE): RealmResults<T>
suspend fun <T : RealmObject> RealmResults<T>.subscribe(mode: WaitForSync = WaitForSync.FIRST_TIME, timeout: Duration = Duration.INFINITE): RealmResults<T>

Automatically create an anonymous Subscription from a local query result in the background and return the result of re-running the same query against the Realm file. This behaves the same as creating a named variant by calling subscribe. See this method for details about the exact behavior.

Return

The result of running the query against the local Realm file. The results returned will depend on which mode was used.

Parameters

mode

mode used to resolve the subscription. See WaitForSync for more details.

timeout

How long to wait for the server to return the objects defined by the subscription. This is only relevant for WaitForSync.ALWAYS and WaitForSync.FIRST_TIME.

Throws

TimeoutCancellationException

if the specified timeout was hit before a query result could be returned.

if this method is called on a Realm that isn't using Flexible Sync.


suspend fun <T : RealmObject> RealmResults<T>.subscribe(name: String, updateExisting: Boolean = false, mode: WaitForSync = WaitForSync.FIRST_TIME, timeout: Duration = Duration.INFINITE): RealmResults<T>

Automatically create a named Subscription from a local query result in the background and return the result of re-running the same query against the Realm file.

This is a more streamlined alternative to doing something like this:

fun suspend getData(realm: Realm): RealmResults<Person> {
val results = realm.query<Person>().find()
realm.subscriptions.update { bgRealm ->
add("myquery", results.query(""))
}
realm.subscriptions.waitForSynchronization()
return realm.query<Person>().find()
}

It is possible to define whether or not to wait for the server to send all data before running the local query. This is relevant as there might be delay from creating a subscription to the data being available on the device due to either latency or because a large dataset needs be downloaded.

The default behaviour is that the first time subscribe is called, the query result will not be returned until data has been downloaded from the server. On subsequent calls to subscribe for the same query, the query will run immediately on the local database while any updates are downloaded in the background.

Return

The result of running the query against the local Realm file. The results returned will depend on which mode was used.

Parameters

name

name of the subscription. This can be used to identify it later in the SubscriptionSet.

mode

mode used to resolve the subscription. See WaitForSync for more details.

timeout

How long to wait for the server to return the objects defined by the subscription. This is only relevant for WaitForSync.ALWAYS and WaitForSync.FIRST_TIME.

Throws

TimeoutCancellationException

if the specified timeout was hit before a query result could be returned.

if this method is called on a Realm that isn't using Flexible Sync.

if the server did not accept the set of queries. The exact reason is found in the exception message.