Firebase连接检测在60秒后不起作用 [英] Firebase connection detection does not work after 60 seconds

查看:75
本文介绍了Firebase连接检测在60秒后不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如一些答案中所述:

在Android上,Firebase自动管理连接状态以减少带宽和电池消耗.如果客户端没有活动的侦听器,没有挂起的写操作或onDisconnect操作,并且没有通过goOffline方法显式断开连接,则Firebase会在闲置60秒后关闭连接.

On Android, Firebase automatically manages 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.

问题是,在60年代以后,即使我参加了一个具有完整的新引用,事件侦听器等的活动之后.它仍然说它是断开连接的,而实际上却不是.

The problem is that after 60s, even after I go to an activity with a complete new reference, event listener, etc.. It still says it is disconnect, when in fact, it is not.

val connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected")
var connectListener : ValueEventListener? = null

fun checkConnection() {
    connectListener = connectedRef.addValueEventListener(object : ValueEventListener {
        override fun onDataChange(snapshot: DataSnapshot) {
            val connected = snapshot.getValue(Boolean::class.java)!!
            if (connected) {
                Log.d("FRAG", "CONNECTED")
            else{
                Log.d("FRAG", "DISCONNECTED")
            }
        }

        override
        fun onCancelled(error: DatabaseError) {
            System.err.println("Listener was cancelled")
        }
    })
}

override fun onDetach() {
    super.onDetach()
    if (connectListener != null){
        connectedRef.removeEventListener(connectListener)
    }
}

如何确保我维护或建立与Firebase的新连接?我在每个片段的onAttach和一个活动的onStart都调用checkConnection方法.

How can I make sure I maintain or create a new connection to Firebase? I call the checkConnection method every onAttach of a fragment and onStart of an activity.

推荐答案

如果您对从服务器读取的任何数据都具有活动的侦听器,则该连接应保持打开状态,除非您在代码中明确调用了goOffline() .请注意,.info/connected本身不需要从服务器读取,因此不会保持连接打开.

If you have an active listener on any data that is read from the server, the connection should remain open unless you've explicitly called goOffline() in your code. Note that .info/connected itself does not require reading from the server, so does not keep the connection open.

似乎您正在使用实时数据库在其他基于Firestore的应用程序上构建状态系统.在这种情况下:Cloud Firestore使用基于gRPC的协议在客户端和服务器之间进行通信,而Firebase Realtime Database使用Web套接字.它们绝不兼容甚至不具有可比性.在Firestore中保持对数据的活动侦听器不会保持与RTDB的连接打开.这就是为什么Firestore文档中的示例还要写一个实际数据节点的原因到实时数据库.

It seems you're using the realtime database to build an presence system on an otherwise Firestore based app. In that case: Cloud Firestore uses a gRPC-based protocol to talk between client and server, while the Firebase Realtime Database uses web sockets. They're in no way compatible or even comparable. Keeping an active listener on data in Firestore does not keep a connection to RTDB open. That's why the example in the Firestore documentation also writes an actual data node to the realtime database.

这篇关于Firebase连接检测在60秒后不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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