Cant Partially Sync Realm


#1

Hello,

I am strugging to get my Query based sync working, It returns an empty object, I have upgraded the Realm to a reference realm. And here is my code, I can see in my Realm that all of my objects are full of data.


function getSyncedConfig(user, syncUrl) {
  return user.createConfiguration({
    schema: [
      AccessRecord,
      ScanRecord,
      AccessPermissions,
      AccessPermissionAccessRecords,
      AccessPermissionLocations
    ],
    schemaVersion: 2,
    sync: {
      url: syncUrl,
      user
    }
  });
}

function openSynced(locationId, apiKey) {
  return new Promise((resolve, reject) => {
    const user = Realm.Sync.User.current;
    Realm.Sync.setLogLevel('trace');
    const realmUrl = 'https://my-realm-url';
    let syncUrl = 'realms://my-realm-url/realm-db/default';
    if (user) {
      try {
        const realm = new Realm(getSyncedConfig(user, syncUrl));
        resolve(realm);
      } catch (e) {
        reject(e);
      }
    } else {
      loginRealm(realmUrl, apiKey).then(newUser => {
        try {
          const realm = new Realm(getSyncedConfig(newUser, syncUrl));
          resolve([realm]);
        } catch (e) {
          reject(e);
        }
      });
    }
  });
}


openSynced(locationId, apiKey)
        .then(syncedRealm => {
          if (syncedRealm.objects('access_records').length === 0) {
            dispatch({
              type: 'noRecords'
            });
          } else if (syncedRealm.objects('access_records').length) {
            setSyncedRealm(syncedRealm);

The payload that is returned from ‘openSynced’ returns an empty object. In this case the user has already been sucessfully logged in.
Can anyone help? Is there any other data you need?


#2

With query based sync you will have to wait for the server to populate the Realm. The last part should look something like:

openSynced(locationId, apiKey).then(syncedRealm => {
  let objs = syncedRealm.objects('access_records');
  let sub = objs.subscribe();
  sub.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;
    }
  });
});

You can found details about the API at https://realm.io/docs/javascript/2.29.2/api/Realm.Sync.Subscription.html and an introduction to query based sync at https://docs.realm.io/sync/using-synced-realms/syncing-data#query-based-synchronization.


#3

Thanks @kneth

The state gets stuck in Realm.Sync.SubscriptionState.Pending

By the definition: The subscription was created, but has not yet been processed by the sync server.

How would I go about further debugging this? I cant see any meaningful information in the Realm Cloud Trace Logs


#4

“Pending” means that the server is still processing your query-based Realm. How many objects do you expect that your Realm should have?


#5

“Pending” means that the server is still processing your query-based Realm. How many objects do you expect that your Realm should have?