问题描述
我目前正在重新开发我的一个应用程序,该应用程序通过从网站而不是官方 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 DB 请求解析后的数据.
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 实时数据库中的数据同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!