1

I have the following code:

func OnlineStatus(userID: String){
        handle = Auth.auth().addStateDidChangeListener { (auth, user) in
            if let user = user {
                // User is signed in.
                self.UID = user.uid

                self.connectedRef.observe(.value, with: { snapshot in
                    if let connected = snapshot.value as? Bool, connected {
                        // print("############################ Connected")
                        self.ref.child(self.UID!).child("OnlineStatus").setValue("ON")
                    } else {
                        // print("############################ Not connected")
                        self.ref.child(self.UID!).child("OnlineStatus").setValue("OFF")
                    }
                    self.ref.child(self.UID!).child("OnlineStatus").onDisconnectSetValue("OFF")
                })
            }}
            }

The function will be triggered in viewWillAppear. The idea is to build a simple presence system. For some reason onDisconnect gets fired when I send the app to background and than send my iPhone to sleep. I actually would like that online status goes to off only when user logs out or looses internet connection. What is wrong with my code or settings?

picciano
  • 22,341
  • 9
  • 69
  • 82
DU.DE
  • 57
  • 8
  • 1
    For detecting logout and connection loss, see https://stackoverflow.com/questions/39169447/logging-a-user-out-with-firebase-3-and-swift-still-shows-the-currentuser and https://stackoverflow.com/questions/41661137/how-to-detect-internet-connection-with-firebase-database-using-swift3 – Paul Beusterien Jan 09 '18 at 23:49

1 Answers1

2

The onDisconnect event fires when the client disconnects from the Firebase Database servers, and that happens when your app goes to the background. There is no difference from Firebase's perspective between the user being on train that drives into a tunnel, and their phone going to sleep. In both cases the connection between the client and the server gets dropped, so the onDisconnect() fires.

You'll typically end up using .info/connected and onDisconnect() to set a value of when the user was last seen, while using onAuthStateChanged() to set a status flag of the user being signed in. Then you show the list of users by first showing users that are signed in, in the order of how recently they were active.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    How can I manage that .info/connected is permanently listening? It does not get fired when I put the phone on flight mode. Would it make sense to pack it somewhere in appdelegate? – DU.DE Jan 12 '18 at 21:26
  • 1
    Phone operating systems work hard to ensure maximum battery life. One way they do that is by aggressively closing connections between apps when the user is not actively using them. So if `.info/connected` is `false` while the app is backgrounded, it is very likely that it is correct, and the connection between the app and the server was severed. – Frank van Puffelen Jan 12 '18 at 22:30