如何同步与火力地堡离线数据库时设备在线? [英] How to sync offline database with Firebase when device is online?

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

问题描述

我目前使用angularJS和PhoneGap的建立为Android / iOS的测试应用程序。

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

该应用程序使用存储在一个数据库火力地堡的纯文本数据。我希望应用程序有自己的本地数据库(在设备离线使用)和一段时间(当设备在线)
用火力地堡数据库同步。

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 /科尔多瓦的存储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 ?

我如何能做到这一点任何线索?上一次类似的问题有人问,得到的答案是没有... <一个href=\"http://stackoverflow.com/questions/14670394/does-firebase-allow-an-app-to-start-in-offline-mode\">(here)...因为它集中于一个假想火力地堡特征

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.

推荐答案

如果火力地堡是在网上开始和暂时失去其连接,然后再重新连接时,它会同步本地数据即可。所以在很多情况下,一旦火力地堡在线,您可以简单地保持在停电时推向火力地堡。

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 /连接来知道什么时候火力地堡连接。

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

的方式与当前的火力地堡工具实现这一点,直到它支持真正的离线存储,将

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


  1. 保存本地数据简单的小

  2. 当设备联机,本地存储的数据转换成JSON

  3. 使用设置()来将数据导入火力地堡保存在适当的路径

  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()来初始化的数据。然后你可以使用火力地堡正常(就好像它是在线),直到它在未来的某个时刻在线自带(你也想存储在本地副本来处理的情况下不可能发生)。

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆