gpt4 book ai didi

Android - Firestore/Firebase 实时数据库 ".info/connected"返回错误的连接状态

转载 作者:搜寻专家 更新时间:2023-11-01 09:22:45 25 4
gpt4 key购买 nike

我在我的应用中使用 Firestore Beta,我想检查是否存在与 Firestore 数据库的有效在线连接。文档说目前没有直接的方法来查询连接状态,但是可以使用 Firebase 连接状态作为一种解决方法。相关的代码 fragment 也可以在文档的这一部分中找到:Build presence in Cloud Firestore .

    connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
connectionStateListener = connectedRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
Boolean connected = snapshot.getValue(Boolean.class);

if (connected == null) {
changeState(FirebaseConnectionState.error);
return;
}

if (connected) {
changeState(FirebaseConnectionState.connected);
} else {
changeState(FirebaseConnectionState.not_connected);
}
}

@Override
public void onCancelled(@NonNull DatabaseError error) {
L.i("FirebaseConnectionStateListener was cancelled");
}
});

ValueEventListener 本身正在工作,我得到了状态更新。但由于某种原因,状态在 1 分钟后从“已连接”切换为“未连接”。除了这是不正确的,我无法将其恢复为“已连接”,除非重新启动应用程序。

这是一些调试输出:

2018-11-15 13:38:20.340: [FirebaseHeartbeatConnector] FirebaseHeartbeatConnector() called with: firebaseApp = [FirebaseApp{name=development, options=FirebaseOptions{applicationId=1:xxxxx:android:xxxxx, apiKey=xxxxx, databaseUrl=null, gcmSenderId=null, storageBucket=xxxx.appspot.com, projectId=xxxxx}}]
2018-11-15 13:38:20.480: [FirebaseHeartbeatConnector] startConnecting() called
2018-11-15 13:38:20.482: [FirebaseHeartbeatConnector] doSendFirestoreHeartbeatRequest() called
2018-11-15 13:38:20.840: [FirebaseHeartbeatConnector] change State called
2018-11-15 13:38:20.840: [FirebaseHeartbeatConnector] State changed initial -> not_connected
2018-11-15 13:38:21.380: [FirebaseHeartbeatConnector] change State called
2018-11-15 13:38:21.381: [FirebaseHeartbeatConnector] State changed not_connected -> connected
2018-11-15 13:39:20.514: [FirebaseHeartbeatConnector] change State called
2018-11-15 13:39:20.515: [FirebaseHeartbeatConnector] State changed connected -> not_connected

这是为什么?我怎样才能获得正确的状态更新?

最佳答案

这可能是由于 Firebase 因不活动而关闭连接造成的,这种情况只发生在 Android 上。来自Detecting Connection State文档:

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.

由于您的应用并未真正使用实时数据库(您使用的是 Cloud Firestore),因此不会有任何其他写入操作或附加的其他监听器,因此连接会自动关闭。您可能会在 logcat 中看到如下内容:

V/FA: Inactivity, disconnecting from the service

作为一种可能的解决方法,您可以尝试将监听器附加到实时数据库中的任何节点以保持连接处于 Activity 状态(任何情况都可以,即使它实际上并不存在):

FirebaseDatabase.getInstance().getReference("keepalive").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
// Do nothing - this listener just keeps the connection alive
}

@Override
public void onCancelled(@NonNull DatabaseError error) {
Log.i("Keepalive listener was cancelled");
}
});

或者,按照 Frank van Puffelen 的建议,您可以通过使用 keepSynced() 告诉它保持整个实时数据库同步来欺骗 Firebase 挂起连接。在空引用上:

FirebaseDatabase.getInstance().getReference().keepSynced(true);

不过,当您确实想将用户的存在标记为已断开连接时,您需要记住移除 keepalive 监听器或撤消 keepSynced()

最后,对于这两个示例,您仍然需要附加并使用 .info/connected connection state listener处理连接状态的变化:

For many presence-related features, it is useful for your app to know when it is online or offline. Firebase Realtime Database provides a special location at /.info/connected which is updated every time the Firebase Realtime Database client's connection state changes. Here is an example:

DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
boolean connected = snapshot.getValue(Boolean.class);
if (connected) {
System.out.println("connected");
} else {
System.out.println("not connected");
}
}

@Override
public void onCancelled(DatabaseError error) {
System.err.println("Listener was cancelled");
}
});

关于Android - Firestore/Firebase 实时数据库 ".info/connected"返回错误的连接状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53320480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com