关闭 Firebase 实时数据库连接当应用关闭时

huangapple go评论64阅读模式
英文:

Close Firebase Realtime Database connection when app is closed

问题

有可能在你的活动关闭时执行代码吗?如何关闭实时数据库连接?

英文:

Is there a possibility to execute code when your activity is closed? And how do I close Realtime Database connections?

答案1

得分: 0

当您要关闭与实时数据库的连接时,是的,可以使用FirebaseDatabase#goOffline()方法:

关闭我们与Firebase数据库后端的连接,直到调用goOnline为止。

但我认为您可能不是要让连接下线,因为它只会影响数据库查询的工作方式。因此,如果您使用这种方法,那么您需要使用goOffline / goOnline自行管理连接状态。此外,根据有关检测连接状态的官方文档:

在Android上,Firebase会自动管理连接状态以减少带宽和电池使用量。当客户端没有活动侦听器,没有待处理的写入或onDisconnect操作,并且没有被明确断开连接的goOffline方法时,Firebase会在60秒的不活动后关闭连接。

因此,无需自行执行此操作,因为Firebase会自动为您执行。

我认为您正在寻找的是读取数据并在那之后分离侦听器。可以使用Query#addListenerForSingleValueEvent(@NonNull ValueEventListener listener)来实现这一点,它会在从数据库获取值后分离侦听器,因此不会持续作为活动侦听器存在。如果您使用此方法获取所有数据,则在大约一分钟的不活动后,连接将被关闭。如果要自行监视连接状态,那么可以使用官方文档中有关检测连接状态的说明中的.info/connected上的侦听器。

还请注意,可以使用Query#get()来实现相同的效果。

如果您需要持久侦听器,则应根据您的活动生命周期相应地分离侦听器。还请注意,@HarkishanPansuriya的解决方案不太正确,因为onDestroy并不总是被调用

在关闭活动时是否可以执行代码?

是的,您可以使用服务来实现这一点。您可以使用后台服务来在后台处理数据。

英文:

> How do I close Realtime Database connections?

When you're referring to closing a connection to a Realtime Database, yes, FirebaseDatabase#goOffline():

> Shuts down our connection to the Firebase Database backend until goOnline is called.

But I think that you're not looking to go offline, as all it does, is affects the way that database queries work. So if you're using this approach, then you have to manage the connection state yourself using goOffline/goOnline. Besides that, according to the official documentation regarding detecting connection state:

> On Android, Firebase automatically manages the connection state to reduce bandwidth and battery usage. When a client has no active listeners, no pending write or onDisconnect operations, and is not explicitly disconnected by the goOffline method, Firebase closes the connection after 60 seconds of inactivity.

So there is no need to do that yourself since Firebase automatically does that for you.

What I think you're looking for is to read the data and detach a listener right after that. That can be achieved using Query#addListenerForSingleValueEvent(@NonNull ValueEventListener listener) which detaches the listener after the values from the databases are obtained and thus does not persist as an active listener. If you fetch all the data using this method, the connection will be closed after about a minute of inactivity. If you want to monitor the connection state yourself, then you can use a listener at .info/connected as explained in the official documentation regarding detecting connection state.

Please also note that the same thing can be achieved using Query#get().

If you need a persistent listener, then you should detach the listener accordingly to the life cycle of your activity. Also note, that @HarkishanPansuriya solution is not quite correct, because onDestroy is not always called.

> Is there a possibility to execute code when your activity is closed?

Yes, you can, using a service. You can use a background service that can process data in the background.

huangapple
  • 本文由 发表于 2023年5月11日 01:53:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定