Search code examples
realmrealm-mobile-platform

Realm: Notification after initial sync


According to the docs Realm can notify you when certain actions are taking place like "every time a write transaction is committed". I am using the Realm Object Server and the first time a user opens my app a large set of data is synched from the server down to the app. I would like to show a loading screen and not present the main UI of my app until Realm has completed its initial sync. Is there a way to be notified / determine when this process is complete?


Solution

  • The realm.io website just posted documentation on how to do this.

    Asynchronously Opening Realms

    If opening a Realm might require a time-consuming operation, such as applying migrations or downloading the remote contents of a synchronized Realm, you should use the openAsync API to perform all work needed to get the Realm to a usable state on a background thread before dispatching to the given queue. You should also use openAsync with Realms that are set read-only.

    For example:

    Realm.openAsync({
      schema: [PersonSchema],
      schemaVersion: 42,
      migration: function(oldRealm, newRealm) {
        // perform migration (see "Migrations" in docs)
      }
    }, (error, realm) => {
      if (error) {
        return;
      }
      // do things with the realm object returned by openAsync to the callback
      console.log(realm);
    })
    

    The openAsync command takes a configuration object as its first parameter and a callback as its second; the callback function receives a boolean error flag and the opened Realm.

    Initial Downloads

    In some cases, you might not want to open a Realm until it has all remote data available. In such a case, use openAsync. When used with a synchronized Realm, this will download all of the Realm’s contents before the callback is invoked.

    var carRealm;
    Realm.openAsync({
      schema: [CarSchema],
      sync: {
        user: user,
        url: 'realm://object-server-url:9080/~/cars'
      }
    }, (error, realm) => {
      if (error) {
        return;
      }
      // Realm is now downloaded and ready for use
      carRealm = realm;
    });