Firebase连接管理器应只返回一个结果 [英] Firebase Connection manager should return only one result

查看:91
本文介绍了Firebase连接管理器应只返回一个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下文档位于以下位置: https ://www.firebase.com/docs/ios/guide/offline-capabilities.html#section-connection-state



然而,我的实施和测试:

  connectionCheck.observeEventType(.Value,withBlock:{snapshot in 
let connected = snapshot.value as ?bool
如果已连接!= nil&& connected!{
print(Connected)
} else {
print(Not connected)





$ b $ p $ X $的输出注释:

 未连接
连接

如果我关掉了wifi,结果很简单:

 未连接

解决方案

作为建议,您可能需要添加一些附加功能;



如果您想了解用户与Firebase的连接,请注意.info / connected路径,如文档中所述。

我使用的设计模式是设置一个名为isConnected的全局应用程序变量,并将Firebase观察者附加到.info / connected路径。

然后,在我的应用程序中,无论我需要知道用户是否连接,我都将KVO Observer附加到我的全局isConnected var。



当用户断开连接(或连接)时,Firebase观察者被调用哪些设置isConnected = false(或者true)。

然后,在我的应用程序中,观察isConnected var的任何地方都会收到断开/连接的通知,并且可以采取适当的措施。



下面是代码

  let connectedRef = rootRef.childByAppendingPath(。info / connected)

connectedRef.observeEventType(.Value,withBlock:{snapshot in

self.isConnected = snapshot.value as! Bool // KVO属性

//这只是所以你可以看到它正在被设置,删除
if(self.isConnected == true){
print(connected )
} else {
print(not connected)
}
//测试代码
})


I am following documentation located at: https://www.firebase.com/docs/ios/guide/offline-capabilities.html#section-connection-state

However, my implementation and test of:

        connectionCheck.observeEventType(.Value, withBlock: { snapshot in
            let connected = snapshot.value as? Bool
            if connected != nil && connected! {
                print("Connected")
            } else {
                print("Not connected")
            }
        })

The output in Xcode notes:

Not connected
Connected

If however I turn off the wifi, the result is simply:

Not connected

Given I wish to allow actions to occur and present to the user if there is not a connection, how can I ensure that this Firebase listener only returns the correct response once?

解决方案

As a suggestion, you may want to add some additional functionality;

If you want to know about the users connection to Firebase, you should observe the .info/connected path, as stated in the docs.

The design pattern I use is to set up a global app variable called isConnected, and attach a Firebase observer to the .info/connected path.

Then in my app wherever I need to know if the user is connected or not, I attach a KVO Observer to my global isConnected var.

When the user disconnects (or connects), the Firebase observer is called which sets isConnected = false (or true).

Then, any places in my app that are observing the isConnected var is notified of the disconnect/connect and can take appropriate action.

Here's the code

let connectedRef = rootRef.childByAppendingPath(".info/connected")

connectedRef.observeEventType(.Value, withBlock: { snapshot in

  self.isConnected = snapshot.value as! Bool //KVO property

        //this is just so you can see it's being set, remove
        if ( self.isConnected == true ) {
            print("connected")
        } else {
            print("not connected")
        }
        // testing code
})

这篇关于Firebase连接管理器应只返回一个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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