Firebase实时数据库.info/connected应该为True时为False [英] Firebase realtime database .info/connected False when it should be True

查看:41
本文介绍了Firebase实时数据库.info/connected应该为True时为False的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android服务,可在 onCreate 上调用此服务:

I have an Android service that calls this at onCreate:

FirebaseDatabase database = FirebaseDatabase.getInstance();
database.getReference(".info/connected").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
        Log.d(TAG, "connected: " + snapshot.getValue(Boolean.class));
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
        Log.w(TAG, "Failed to read value.", error.toException());
        }
});

我注意到,当我进行大量的wifi和蜂窝数据切换时,最终会看到"connected:false"消息,而没有"connected:true"消息.除了Firebase实时数据库外,我还在该服务中运行Firestore,并且Firestore此时已正确连接.

I've noticed that when I do some amount of toggling wifi and cellular data, I eventually see a "connected: false" message and no "connected: true" message. Along with the Firebase realtime database, I'm also running Firestore in the service, and Firestore is properly connecting at this point.

然后我触发Android服务以运行以下代码:

I then trigger the Android service to run this code:

FirebaseDatabase.getInstance().getReference("random/data").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
           // This method is called once with the initial value and again
           // whenever data at this location is updated.
           boolean connected = snapshot.getValue(Boolean.class);
           Log.d(TAG, "random data: " + connected);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
           // Failed to read value
           Log.w(TAG, "cancelled system/online.", error.toException());
        }
});

现在,我成功读取了 并打印了"connected:true".

And now, I get a successful read and "connected: true" is printed.

发生了什么事?为什么需要从Firebase读取以触发 .info/connected ?

What's happening? Why do I need to read from firebase for .info/connected to trigger?

推荐答案

为什么需要从firebase读取 .info/connected 才能触发?

答案留在官方文档中:

Firebase Realtime数据库在/.info/connected 中提供了一个特殊位置,该位置在Firebase Realtime Database客户端的连接状态每次更改时都会更新.

Firebase Realtime Database provides a special location at /.info/connected which is updated every time the Firebase Realtime Database client's connection state changes.

/.info/connected 是一个布尔值,在实时数据库客户端之间同步,因为该值取决于客户端的状态.换句话说,如果一个客户端将/.info/connected 读为false,则不能保证单独的客户端也将读为false.

/.info/connected is a boolean value which is not synchronized between realtime database clients because the value is dependent on the state of the client. In other words, if one client reads /.info/connected as false, this is no guarantee that a separate client will also read false.

在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.

因此,在Android上,您还可以利用连接状态管理.因此,一旦实现了上述解决方案,您将看到SDK动态地管理了这种方式,如果没有连接侦听器并且没有使用 setValue()进行写操作,则连接会自动断开连接.在过去60秒钟内在应用程序中运行,但是 ValueEventListners 的存在将覆盖此设置,并确保与数据库的持续连接.您还可以在此内查看答案.发布.

So on Android, you can also take advantage of the connection state management. So once you implement the above solution, you'll see that the SDK manages this dynamically in a way that the connections disconnect automatically if there are no listeners attached and if no write operations using setValue() are made in the app in last 60 seconds, but the presence of a ValueEventListners will override this and will ensure continuous connectivity with the database. You can also take a look at answers within this post.

还有另一条帖子我建议您阅读以更好地理解.

There is also another post that I recommend you to read for a better understanding.

这篇关于Firebase实时数据库.info/connected应该为True时为False的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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