[Realm React] Offline, Logged In Users

Hello, me again with yet another question about Realm-React. This time it regards users.

Use Case: I would like to build an application with the following behavior:

  • If a user is not logged in, they have access to an Offline Realm.
  • If a user is logged in, but they are not subscribed, they have access to the same Offline Realm—only their writes have a corresponding userId tag.
  • If a user is logged in and have a subscription, they have access to an Online Realm with flexible sync.

So far, I have my app setup as such:

<UserProvider fallback={AppNoSync} />
  <AppSync />
</UserProvider>

AppNoSync is set up like so:

<RealmProvider>
  <AppStuff />
</RealmProvider>

and AppSync is similarly set up, like so:

const user: User = useUser();
const syncConfig: Partial<Realm.SyncConfiguration> | undefined =
    useMemo(() => {
      return {
        user: user,
        flexible: true,
        onError: (error: any) => {
          console.log("app sync error");
          console.error(error);
        },
      };
    }, [user]);

<RealmProvider sync={syncConfig}>
  <AppStuff />
</RealmProvider>

This also works without defining user in the syncConfig.

The current behavior is like so:

  • If a user is not logged in, they have access to the Offline Realm.
  • If a user is logged in, they have access to the Flexible Sync Realm.

However, when I try to conditionally render the Offline Realm version based on user subscription OR when I try to conditionally set the RealmProvider, I encounter the following error:

Error: Exception in HostFunction: Access to invalidated Results objects

Is there a way I can configure sync to accommodate offline users? How can I preserve the Offline Realm if a user has logged in but has not subscribed?

I am not completely familiar with this error, and so far as I know, I only encounter this error when I attempt to implement the Offline Realm with a User logged in. For instance, if my AppSync component does not have a syncConfig, my app crashes and I encounter this error. If I conditionally set syncConfig, I run into the same error.

How can I implement offline functionality with a user logged in?

1 Like

Did you manage to find a solution to this?