在Android和FCM的背景上处理keepSynced() [英] Handling keepSynced() while on background on Android and with FCM

查看:163
本文介绍了在Android和FCM的背景上处理keepSynced()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑为Firebase实时数据库中的某些数据使用 keepSynced()。我知道它会自动同步这些路径。但是,这与Android生命周期有什么关系呢?如果用户离开所有活动(并且所有正常的听众断开连接),它会停止同步吗?我不希望应用程序成为数据或电池。另一方面,我想在FCM通知到达时更新缓存的数据。我可以启动一些将连接到Firebase的服务。我想同步 keepSynced()中的所有路径,并在同步时停止它。我不知道如何实现这一点。创建一个监听器的路径之一,并保持运行一段时间的服务?服务完成后,会停止同步吗?

b
$ b

好的问题!

如果没有活动活动,操作系统可能会随时关闭与Firebase数据库的连接。我们的SDK不会试图阻止这种情况发生,但当应用再次激活时, 重新连接。 段落就是我们所说的推送同步,在这里你发送一个推送通知(通常是一个无声的FCM数据消息)来触发数据的同步。

就像在去年的I / O应用程序中一样,虽然比我们想要的要复杂一点,但是效果很好。我们在这种情况下显式管理连接,调用 goOnline() goOffline()(5分钟后)。主同步代码可以在 IOSched github repo


I'm considering the use of keepSynced() for some data from Firebase Realtime Database. I understand that it will automatically sync those paths. But how does that relate to Android lifecycle? If the user leaves all activities (and all normal listeners disconnect), will it stop syncing? I don't want the app to become data or battery hog.

On the other hand, I would like to update cached data when FCM notification arrives. I can launch some service which will connect to Firebase. I would like to sync all paths which are in keepSynced() and stop it when it's synced. I'm not sure how to achieve that. Create a listener to one of the paths and keep the service running for some time? After the service is finished, will it stop syncing?

解决方案

firebaser here

Great question!

When there is no active activity, the operating system may close the connection to the Firebase database at any time. Our SDKs don't try to prevent that, but will reconnect when the app becomes active again.

What you're describing in your second paragraph is what we call "push to sync", where you send a push notification (typically a silent FCM data message) to trigger synchronizing of the data.

We did something like that in last year's I/O app and, while it was a bit more complex than we wanted it to be, it worked great. We explicitly managed the connection in that case, calling goOnline() and goOffline() (after 5 minutes iirc). The main sync code can be found in the IOSched github repo.

这篇关于在Android和FCM的背景上处理keepSynced()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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