【问题标题】:Realm: Can a local realm exist while using a server synced realm?领域:在使用服务器同步领域时可以存在本地领域吗?
【发布时间】:2017-07-16 20:22:44
【问题描述】:

我有一个运行良好的服务器同步领域。我想添加一个额外的本地领域来仅在本地存储一些项目:

  this.userRealm = new Realm({
    path: 'userRealm.realm',
    schema: [cgps_schema.DirectoryFavoritesSchema],
  });

这似乎不起作用。也许不是有意的?

如果我在尝试连接到我的同步领域之前调用 new Realm(),它会创建 userRealm.realm.management 目录和 userRealm.realm.lock 文件,但不会创建 userRealm.realm 文件。如果我在连接到我的同步领域后调用 new Realm(),它会创建所有文件并正常工作,但是当我重新加载应用程序时,它会删除 userRealm.realm 并创建一个新的空白。

【问题讨论】:

  • 如何打开同步领域?它是否与未同步的本地路径相同?
  • 我使用上面发布的代码打开它,该代码提供了默认领域的替代路径。
  • 它刚刚开始工作。

标签: realm realm-mobile-platform


【解决方案1】:

当你打开不同的领域时,你应该使用不同的path。下面是一些打开 1 个同步领域和 1 个未同步领域的代码:

const Realm = require('realm');

const ItemSchema = {name: 'Item', properties: {id: 'int', name: 'string'}};

const unsynced = new Realm({
    path: 'unsynced.realm',
    schema: [ItemSchema],
})

Realm.Sync.User.register('http://localhost:9080', 'user1', 'pass1', (error, user) => {
    const synced = new Realm({
        path: 'synced.realm',
        schema: [ItemSchema],
        sync: {
            url: 'realm://localhost:9080/~/synced',
            user: user,
        },
    })
    synced.close();
    user.logout();
    unsynced.close();
})

【讨论】:

  • 我的代码现在可以工作了,不知道为什么,因为我没有真正改变任何东西。但这是正确的方法,所以标记正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多