Syncing with background fetch

Is anyone aware of a way to allow a flexible sync realm to update and push local changes via background fetch?

I’ve had no issues opening a local realm in a background fetch, but it seems a flexible synced realm is blocked from being accessed. i.e.

Realm at path '/Users/XXXXXX/Library/Developer/CoreSimulator/Devices/2EBE2645-817B-403D-B4EE-47E78EB4B528/data/Containers/Data/Application/5F9BDEE3-CC08-4D96-8BFA-2EACE9EFE52B/Documents/mongodb-realm/xxxxx-xxxx-jkjzx/6485f1fb856423dcdb19f612/flx_sync_default.realm' already opened with different sync user

In theory, I am opening the realm with the same user and the exact same config. i.e.

  const app = Realm.App.get(appId);
  const realmConfiguration: Realm.Configuration = {
    ...realmConfig,
    sync: {
      ...realmSyncConfig,
      user: app.currentUser
    },
  };

Anyone else achieved this?

In the end worked this out with the support team, and the comments from this PR: https://github.com/realm/realm-js/issues/6461

For anyone else that comes across this issue, this code snippet works with the expo-background-fetch package for sure, and likely any similar method of background fetch:

const app = Realm.App.get(appId);
const currentUser = app.currentUser;
if (!currentUser?.id) {
  // Exit, user isn't authenticated
  throw new Error('Unauthenticated');
}

const realmConfiguration: Realm.Configuration = {
  schema: realmConfig.schema,
  sync: {
    user: currentUser,
    flexible: true
  },
  // @ts-ignore
  openSyncedRealmLocally: true,
  path: `mongodb-realm/${appId}/${app.currentUser?.id}/flx_sync_default.realm`
};

let remoteRealm: Realm | null = null;
try {
  remoteRealm = await Realm.open(realmConfiguration);
  // Do stuff
} catch (e) {
  // Couldn't open realm :(
}

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.