Firebase Connection manager should return only one result

前端 未结 1 1675
渐次进展
渐次进展 2021-01-15 14:21

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

However, my implementation and te

相关标签:
1条回答
  • 2021-01-15 14:53

    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
    })
    
    0 讨论(0)
提交回复
热议问题