检测称为两次的连接状态 [英] Detecting Connection State Called Twice

查看:51
本文介绍了检测称为两次的连接状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

let connectedRef = Database.database().reference(withPath: ".info/connected")
connectedRef.observe(.value, with: { snapshot in
   if snapshot.value as? Bool ?? false {
      print("Connected")
   } else {
      print("Not connected")
   }
})

我正在使用它来检测我与Firebase的连接状态.我的问题是,当他们是Internet连接时,结果变为未连接",然后变为已连接".如果他们没有互联网连接,则直接转到未连接".有人可以解释吗?

I'm using this to detect my connection state to my firebase. My problem is when their is internet connection the result goes "Not Connected" then afterwards goes "Connected". When their is no internet connection it just goes directly to "Not Connected". Can someone please explain?

推荐答案

您所看到的是预期的行为.

What you're seeing is the expected behavior.

.info/connected标志确定应用程序/客户端是否已连接到Firebase数据库后端.虽然这当然需要您具有Internet连接,但除此之外,还不止如此.这就是为什么即使您的互联网连接正常,.info/connected仍可能是false的原因.

The .info/connected flag determines whether the app/client is connected to the Firebase Database backend. While this of course requires that you have an internet connection, there is more to it than that. That's why it is possible for .info/connected to be false even though you have a working internet connection.

启动应用程序时尤其如此.在应用程序启动后,Firebase客户端连接到其数据库服务器需要花费一些时间,因此通常.info/connected值以false开头,然后变为true.有时,它甚至会在稳定之前切换几次.

This is especially true when you start the app. It takes a few moments after the app starts before the Firebase client is connected to its database server, so usually the .info/connected value starts as false and then becomes true. Sometimes it even toggles a few times before settling.

另请参阅:

  • How to handle internet connection status Firebase
  • Firebase connection state listener returns false in javascript

这篇关于检测称为两次的连接状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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