React native 在完全脱机的情况下,如何使用MongoDB同步领域数据库的本地副本?

React native 在完全脱机的情况下,如何使用MongoDB同步领域数据库的本地副本?,react-native,realm,mongodb-realm,React Native,Realm,Mongodb Realm,我正在我的React本机应用程序上使用MongoDB Realm Sync。当我在线启动我的应用程序,然后断开互联网时,我的领域工作正常。我可以看到我的数据,我也可以写数据同步服务器时,我回到网上。但当我完全离线启动应用程序时,我的应用程序不会显示任何数据。据我所知,realm应该读取本地数据库并返回数据,即使应用程序从完全脱机启动时也是如此。不是吗?脱机启动应用程序时,如何访问数据?下面是我用来和服务器同步的代码 const config = { schema: [sessionS

我正在我的React本机应用程序上使用MongoDB Realm Sync。当我在线启动我的应用程序,然后断开互联网时,我的领域工作正常。我可以看到我的数据,我也可以写数据同步服务器时,我回到网上。但当我完全离线启动应用程序时,我的应用程序不会显示任何数据。据我所知,realm应该读取本地数据库并返回数据,即使应用程序从完全脱机启动时也是如此。不是吗?脱机启动应用程序时,如何访问数据?下面是我用来和服务器同步的代码

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);
    }

我在这里找到了类似问题的答案: 您可以执行以下操作:

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"
        }
    };
}

欢迎来到SO。这个问题有点模糊。王国所做的一切都是,这就是王国的意义所在。您的数据总是在本地写入,然后同步到服务器。你能澄清你的问题并给出一些你有困难的代码吗?请花点时间回顾@Jay Hi Jay,我已经编辑了我的问题并添加了用于与mongodb realm server同步的代码。我希望比以前更清楚一点。非常感谢。
const OpenRealmBehaviorConfiguration = {
    type: "openImmediately",
}

const configuration = {
    schema: [UserSchema],
    sync: {
        user: app.currentUser,
        partitionValue: "user_1",
        // Add this two lines below
        newRealmFileBehavior: OpenRealmBehaviorConfiguration,
        existingRealmFileBehavior: OpenRealmBehaviorConfiguration,
    }
}