保持Firebase实时数据库中的数据始终保持同步 [英] Keep data from the Firebase Realtime Database always in sync

查看:163
本文介绍了保持Firebase实时数据库中的数据始终保持同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在重新开发一个应用程序,通过从网站获取原始数据来更新它的内部SQLite数据库,而不是正式的API。

是非常容易出错的,我想将数据处理从客户端移到后端服务器。
想法是每天多次在服务器上运行一个脚本(类似于之前的客户端行为),并将数据存储在Firebase实时数据库中。然后,所有客户端不再需要自己处理数据,而是从Firebase DB请求分析的数据。



我只是想知道保存数据的最佳方法是什么同步。
我提出了两个想法,但由于我对Firebase不太熟悉,所以我不知道最好是什么。



通过在一些节点上使用 keepSynced() setPersistenceEnabled()同步客户机上的数据。
但我不确定这是否会保持数据最新的时候,应用程序在后台。我也担心这种方法的电池和网络使用情况。
第三点是同时连接的数量,我用这个方法用每一个客户端都会连接所有的时间,对不对?第二种方法是使用FCM通知客户每当他感兴趣的节点有变化时,例如,使用Firebase云端函数来侦听事件并触发消息。
然后,客户端将再次同步数据库和 goOffline(),然后 goOnline()这将避免大量的连接以及减少电池和网络使用。
但是我不确定如何在离线时保留设备上的所有数据?
我不想使用SQLite数据库来保存设备上的数据。
setPersistenceEnabled()足以在设备上保存特定数量的数据库注释吗?



是两种方式的结合?在所需节点上设置 keepSynced() setPersistenceEnabled(),并在 goOnline() code> / goOffline()取决于云消息?

任何帮助或建议基于自己的expiriences您的第一个方法是确保Android应用程序始终具有与Firebase数据库后端的开放连接。这已被问及/以前(请参阅这里这里),绝对有可能。但是,你会与Android更新,特别是试图通过这种类型的应用程序的行为,以减少电池使用的战斗。



第二种方法确实更好。我经常将其称为推送同步,因为您正在发送推送通知(通过FCM)来触发数据同步。 (加上:这是一个拉到同步模式的文字播放,所以很多应用程序遵循)。使用此方法的应用程序的一个示例是2016年Google的Android I / O应用程序,Github上的代码。代码有点复杂,但是实际同步在这里和(iirc)它是通过这个触发。


I'm currently re-working an app of mine which updates it's internal SQLite database by fetching raw data from an website, not an official API.

As this is quite error-prone I'd like to move the data processing out of the client into an backend server. The idea is to have a script running on the server multiple times a day - similar to the client behaviour before - and storing the data in a Firebase Realtime Database. Then, all clients no longer have to process the data themselves but rather request parsed data from the Firebase DB.

I'm just wondering what's the best approach to keep the data synced. I came up with two ideas, but as I'm not too familiar with Firebase I don't know what's preferable.

For one I could imagine keeping the data on the client in sync by using keepSynced() and setPersistenceEnabled() on some nodes. But I'm unsure whether this will keep data up to date when the app is in background. As well I'm worried about the battery and network usage of this approach. A third point is the amount of simultaneous connections, I quess with this approach every client will be connected all the time, right?

The second approach would be to use FCM to notify the client whenever there is a change in one of his interested nodes, e.g. using Firebase Cloud Functions to listen for event and trigger the message. Then the client would goOnline() to sync with the database and goOffline() again. This would avoid the huge amount of connections as well as reduce battery and network usage. But I'm unsure on how to keep all the data on the device while offline? I don't want to use a SQLite database to keep data on the device. Is setPersistenceEnabled() sufficient to keep a particular amount of database notes on the device?

Or is a mix of both the way to go? Setting keepSynced() and setPersistenceEnabled() on the needed nodes and goOnline()/goOffline() depending on cloud messages?

Any help or advices based on own expiriences welcome!

解决方案

Your first approach is trying to ensure the Android app always has an open connection to the Firebase Database backend. This has been asked/tried before(see here, here), and is definitely possible. But you'll be fighting with Android updates, which are specifically trying to reduce battery usage by this type of behavior in apps.

The second approach is indeed better. I often refer to it as "push to sync", since you're sending a push notification (through FCM) to trigger a data sync. (plus: it's a word play on the "pull to sync" pattern so many apps follow). An example of an app using this approach is Google's I/O app for Android from 2016, the code for which is on Github. The code is a bit convoluted, but the actual sync is here and (iirc) it is triggered through this.

这篇关于保持Firebase实时数据库中的数据始终保持同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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