3

I am using MongoDB Realm Sync on my React Native app. When I start my app online and later disconnect internet, my realm works fine. I can see my data and also I can write data which syncs with server when I go back online. But when I start my app completely offline, my app does not show any data. From what I understand, realm is suppose to read local database and return data even when the app starts from complete offline. Isn't it ? How can I access my data when I start my app offline ? Below is my code I've used to sync with server.

const config = {
      schema: [sessionSchema],
      sync: {
        user,
        partitionValue: 'Test',
      },
    };

try {
      Realm.open(config)
        .then((openedRealm) => {
          if (canceled) {
            openedRealm.close();
            return;
          }
          realmRef.current = openedRealm;
          const syncSessions = openedRealm.objects('session');

         openedRealm.addListener('change', () => {
            setSessions([...syncSessions]);
         });
        setSessions([...syncSessions]);
     }
  } catch (e) {
      console.log('ERROR', e);
    }
  • Welcome to SO. The question is a bit vague. Everything Realm does is [offline first](https://docs.mongodb.com/realm/get-started/introduction-mobile/#realm-database), that's the point of Realm. You data is always written locally and *then* sync'd to the server. Can you clarify what you're asking and present some code that you're having difficulty with? Please take a moment and review [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Jay Oct 27 '20 at 14:44
  • @Jay Hi Jay, I've edited my question and added code I've used to sync with mongodb realm server. I hope it is bit clear than before. Thank you ! – user3609554 Oct 27 '20 at 16:02

2 Answers2

1
const OpenRealmBehaviorConfiguration = {
    type: "openImmediately",
}

const configuration = {
    schema: [UserSchema],
    sync: {
        user: app.currentUser,
        partitionValue: "user_1",
        // Add this two lines below
        newRealmFileBehavior: OpenRealmBehaviorConfiguration,
        existingRealmFileBehavior: OpenRealmBehaviorConfiguration,
    }
}
Chintu
  • 31
  • 2
  • 1
    Can you offer an explanation of your approach? How does this differ from @lukast008's existing answer? Why do you prefer this approach? – Jeremy Caney Jun 25 '21 at 18:52
0

I found an answer for similar question here: https://developer.mongodb.com/community/forums/t/open-synced-local-database-when-completely-offline/11169/2 You can do something like:

async function getRealm() {
    const app = new Realm.App("your-app-id");
    if (app.currentUser) {
        // A user had already logged in - open the Realm synchronously
        return new Realm(getConfig(app.currentUser));
    }

    // We don't have a user - login a user and open the realm async
    const user = await app.logIn(Realm.Credentials.anonymous());
    return await Realm.open(getConfig(user));
}

function getConfig(user) {
    return {
        sync: {
            user,
            partitionValue: "my-partition"
        }
    };
}
lukast008
  • 51
  • 1
  • 4