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

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

问题描述

我目前正在重新开发我的一个应用程序,该应用程序通过从网站而非官方 API 获取原始数据来更新其内部 SQLite 数据库.

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.

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

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.

我只是想知道保持数据同步的最佳方法是什么.我想出了两个想法,但由于我对 Firebase 不太熟悉,我不知道哪个更可取.

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.

例如,我可以想象通过在某些节点上使用 keepSynced()setPersistenceEnabled() 来保持客户端上的数据同步.但是我不确定当应用程序在后台时这是否会使数据保持最新.我也担心这种方法的电池和网络使用情况.第三点是同时连接的数量,我怀疑这种方法每个客户端都会一直连接,对吗?

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?

第二种方法是使用 FCM 在其感兴趣的节点之一发生变化时通知客户端,例如使用 Firebase Cloud Functions 监听事件并触发消息.然后客户端将 goOnline() 与数据库同步,然后再次goOffline().这将避免大量连接以及减少电池和网络使用.但我不确定如何在离线时将所有数据保存在设备上?我不想使用 SQLite 数据库将数据保存在设备上.setPersistenceEnabled() 是否足以在设备上保留特定数量的数据库笔记?

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?

还是两者兼而有之?在需要的节点和 goOnline()/goOffline() 上设置 keepSynced()setPersistenceEnabled() 取决于云消息?

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!

推荐答案

您的第一种方法是尝试确保 Android 应用始终与 Firebase 数据库后端保持开放连接.之前已经询问/尝试过(请参阅此处这里),并且绝对有可能.但您将面临 Android 更新,这些更新专门尝试通过应用中的此类行为来减少电池使用量.

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.

第二种方法确实更好.我经常将其称为推送同步",因为您正在发送推送通知(通过 FCM)来触发数据同步.(另外:这是拉动同步"模式的文字游戏,因此许多应用程序都遵循).使用这种方法的应用程序的一个例子是 2016 年 Google 的 Android I/O 应用程序,代码位于 Github.代码有点复杂,但是 实际同步在这里 和 (iirc) 它是 触发

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天全站免登陆