【问题标题】:Realm: Notification after initial sync领域:初始同步后的通知
【发布时间】:2017-06-10 15:27:10
【问题描述】:

根据the docsRealm 可以在某些操作发生时通知您,例如“每次提交写入事务”。我正在使用领域对象服务器,当用户第一次打开我的应用程序时,大量数据从服务器同步到应用程序。我想显示一个加载屏幕,并且在 Realm 完成初始同步之前不显示我的应用程序的主 UI。有没有办法通知/确定此过程何时完成?

【问题讨论】:

  • 什么平台。? android ios react-native xamarin?
  • React Native / JS

标签: realm realm-mobile-platform


【解决方案1】:

realm.io 网站刚刚发布了 documentation 说明如何执行此操作。

异步打开领域

如果打开一个 Realm 可能需要一个耗时的操作,例如应用迁移或下载同步 Realm 的远程内容,您应该使用 openAsync API 来执行所有需要的工作,以使 Realm 在调度到给定队列之前的后台线程。您还应该将 openAsync 与设置为只读的领域一起使用。

例如:

Realm.openAsync({
  schema: [PersonSchema],
  schemaVersion: 42,
  migration: function(oldRealm, newRealm) {
    // perform migration (see "Migrations" in docs)
  }
}, (error, realm) => {
  if (error) {
    return;
  }
  // do things with the realm object returned by openAsync to the callback
  console.log(realm);
})

openAsync 命令的第一个参数是配置对象,第二个参数是回调;回调函数接收一个布尔错误标志和打开的领域。

初始下载

在某些情况下,您可能不想打开 Realm,直到它拥有所有可用的远程数据。在这种情况下,请使用 openAsync。当与同步的 Realm 一起使用时,这将在调用回调之前下载所有 Realm 的内容。

var carRealm;
Realm.openAsync({
  schema: [CarSchema],
  sync: {
    user: user,
    url: 'realm://object-server-url:9080/~/cars'
  }
}, (error, realm) => {
  if (error) {
    return;
  }
  // Realm is now downloaded and ready for use
  carRealm = realm;
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多