设备在线时如何将离线数据库与

设备在线时如何将离线数据库与

本文介绍了设备在线时如何将离线数据库与 Firebase 同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 angularJS 和 phonegap 为 Android/iOS 构建测试应用程序.

I'm currently using angularJS and phonegap to build a test application for Android / iOS.

该应用仅使用存储在 Firebase 数据库中的文本数据.我希望应用程序拥有自己的本地数据库(在设备离线时使用)和某个时候(当设备在线时使用)与 Firebase 数据库同步.

The app use only text data stored in a Firebase database. I want the app to have its own local database (used when the device is offline) and sometime (when the device is online)sync with a Firebase database.

离线模式使用phonegap/cordova的存储API.我可以只检查设备的在线状态并定期备份在线数据库吗?

The offline mode uses the storage API of phonegap/cordova. Could I just check the device's online state and backup the online database periodically ?

关于我如何实现这一目标的任何线索?上次问类似的问题,答案是还没有"... (此处)...因为它专注于假设的 Firebase 功能.

Any clues on how I can achieve this ? Last time a similar question was asked, the answer was "not yet"... (here)... because it focused on a hypothetical Firebase feature.

推荐答案

如果 Firebase 一开始在线,暂时失去连接,稍后重新连接,它会同步本地数据.因此,在许多情况下,一旦 Firebase 上线,您就可以在中断期间继续推送到 Firebase.

If Firebase is online at the start and loses its connection temporarily, then reconnects later, it will sync the local data then. So in many cases, once Firebase is online, you can simply keep pushing to Firebase during an outage.

对于真正的离线使用,您可能需要监控设备的状态,并查看 .info/connected 以了解 Firebase 何时连接.

For true offline usage, you will probably want to monitor the device's state, and also watch .info/connected to know when Firebase connects.

new Firebase('URL/.info/connected').on('value', function(ss) {
   if( ss.val() === null ) /* firebase disconnected */
   else /* firebase reconnected */
});

使用当前的 Firebase 工具集实现这一目标的方法,直到它支持真正的离线存储,将

The way to achieve this with the current Firebase toolset, until it supports true offline storage, would

  1. 保持本地数据简单和小
  2. 当设备上线时,将本地存储的数据转换为 JSON
  3. 使用 set() 将数据保存到 Firebase 的适当路径
  1. keep the local data simple and small
  2. when the device comes online, convert the locally stored data to JSON
  3. use set() to save the data into Firebase at the appropriate path

此外,如果应用在设备离线时加载,出于某种原因,您可以通过调用 set() 来初始化"数据来准备"Firebase.然后,您可以照常使用 Firebase(就像它在线一样),直到它在未来某个时候上线(您还需要存储本地副本以处理它永远不会上线的情况).

Additionally, if the app loads while the device is offline, for some reason, you can "prime" Firebase by calling set() to "initialize" the data. Then you can use Firebase as normal (just as if it were online) until it comes online at some point in the future (you would also want to store your local copy to handle the case where it never does).

显然,越简单越好.并发修改、本地存储大小限制以及许多其他因素会迅速累积,使任何离线存储解决方案变得复杂且耗时.

Obviously, the simpler the better. Concurrent modifications, limits of local storage size, and many other factors will quickly accumulate to make any offline storage solution complex and time consuming.

这篇关于设备在线时如何将离线数据库与 Firebase 同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:43