Flexible sync when device is offline

Hello, Please how can I use Flexible sync in realm with React Native when the device is offline? When I am online, everything works well, but when I go offline, the RealmProvider gets stuck at the fallback without being able to pass through. Here is my code below.

<UserProvider fallback={<FallBackNavigator />}>
      <RealmProvider
        sync={{
          flexible: true,
          initialSubscriptions: {
            update: (subs, realm) => {
                subs.add(realm.objects('courses'),{
                  name: 'allCoursesSubscription',
              })
            },
            rerunOnOpen: true,
          },
        }}
        fallback={() => <ActivityIndicator size={'large'} style={{ position: 'absolute', zIndex: 999, top: DEVICE_HEIGHT * 0.5, left: DEVICE_WIDTH * 0.46 }} />}
      >
        <Navigator
          initialRouteName="Splash"
          screenOptions={{
            headerShown: false,
          }}
        >
          <Screen name="Splash" component={Splash} />
          <Screen name="Onboarding" component={Onboarding} />
          <Screen name="DrawerNavigator" component={DrawerNavigator} />
          <Screen name="BottomNavigator" component={BottomNavigator} />
        </Navigator>
      </RealmProvider>
    </UserProvider>

I have checked other topics on this with no way to get unblocked, I have also read the docs all through.
I only have a problem with it when I am offline. Please help

I finally found out how to solve the Issue I was having. I was forgetting the user field in the sync property of the RealmProvider. So I added it and everything was now okay

1 Like

Hey @Oben_Tabiayuk can you expand how you solved it. @Andrew_Meyer can you advise.
Same with me, when I am online, everything works well, but when I go offline and reopen the app the RealmProvider gets stuck at the fallback without being able to sync data.

<RealmProvider
  sync={{
    flexible: true,
    onError: (_, error) => {
      console.log(error);
    },
  }}
  fallback={LoadingIndicator}>
  <App />
</RealmProvider>

I want to tell Realm to only connect to the remote server if the device is online. If the device is offline, Realm should immediately return the local subscription.

@Siso_Ngqolosi We are planning to update this in v13, but the default behaviors for opening Realm are unfortunately not very offline first friendly. However this can be solved with the following settings:

<RealmProvider
  sync={{
    flexible: true,
    existingRealmBehavior: {
        openBehavior: "openImmediately",
    },
    newRealmFileBehavior: {
        openBehavior: "downloadBeforeOpen",
    },
    onError: (_, error) => {
      console.log(error);
    },
  }}
  fallback={LoadingIndicator}>
  <App />
</RealmProvider>

Let us know if this helps!

Thanks @Andrew_Meyer for that.

Also found this and it seems to be working . Just have to fix the error message is telling me that the type "downloadBeforeOpen" is not assignable to the type Realm.OpenRealmBehaviorType .

const realmAccessBehavior: Realm.OpenRealmBehaviorConfiguration = {
    type: 'downloadBeforeOpen',
    timeOutBehavior: 'openLocalRealm',
    timeOut: 1000,
  };

  return (
   ...
          <RealmProvider
            sync={{
              flexible: true,
              newRealmFileBehavior: realmAccessBehavior,
              existingRealmFileBehavior: realmAccessBehavior,
              onError: (_, error) => {
                console.log(error);
              },
            }}
            fallback={LoadingIndicator}>
...

Unrelated: Enjoyed your talk realm talk here: https://youtu.be/biIPsXoHu7Q?si=WysWpd7PkZbbr7e0

1 Like

@Siso_Ngqolosi use this to solve your error

const existingRealmFileBehaviorConfig: OpenRealmBehaviorConfiguration = {
    type: OpenRealmBehaviorType.OpenImmediately,
  };

  const newRealmFileBehaviorConfig: OpenRealmBehaviorConfiguration = {
    type: OpenRealmBehaviorType.OpenImmediately,
  };

 <RealmProvider
      sync={{
        user: app.currentUser!,
        flexible: true,
        newRealmFileBehavior: newRealmFileBehaviorConfig,
        existingRealmFileBehavior: existingRealmFileBehaviorConfig,
1 Like

Hey @Andrew_Meyer, an idea why this might be happening?

Issue Description

This problem occurs when modifications are made to the local realm database while the application is running in an offline mode (no internet connection). When the application is subsequently brought back online, the changes made to the local database are overwritten by the data stored in MongoDB.

Reproduction Steps

To replicate this issue, follow these steps:

  1. Launch the application and log in. Select your event and proceed to check in a guest.
  2. Disable your phone’s network connection to simulate an offline state.
  3. Close the application (referred to as “the app”).
  4. Reopen the application while still in offline mode.
  5. Perform a guest check-in.
  6. Re-establish your phone’s network connection, bringing the application back online.
  7. Once the application is synchronized with the server, you will observe that the guest’s ticket status has reverted to “not checked in.”
const realmAccessBehavior = {
    type: 'downloadBeforeOpen',
    timeOutBehavior: 'openLocalRealm',
    timeOut: 1000,
  };

  return (
   ...
          <RealmProvider
            sync={{
              flexible: true,
              newRealmFileBehavior: realmAccessBehavior,
              existingRealmFileBehavior: realmAccessBehavior,
              onError: (_, error) => {
                console.log(error);
              },
            }}
            fallback={LoadingIndicator}>
...

@Siso_Ngqolosi Sorry for the late reply, this seems to have fallen through my filter.

This should be working. Can you check the logs in Atlas if there was an error? Perhaps the permissions on this schema are too restrictive. If on the server side, the user didn’t have the rights to modify this object, then they would be reverted on sync.

1 Like

Is this due to it being guest?

Perform a guest check-in

Because the anon auth page has this very big warning:

An Anonymous user object is not intended to persist data. Once a user logs out, the user cannot retrieve any previous user data.

So “once user logs out” seems to be maybe happening in the transition to being a guest user offline then having a connection established?