如果应用程序进入后台模式,则会触发Ondisconnect [英] Ondisconnect is fired if app goes to background mode

查看:731
本文介绍了如果应用程序进入后台模式,则会触发Ondisconnect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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")
                })
            }}
            }

该函数将在viewWillAppear中触发。我们的想法是构建一个简单的在线系统。出于某种原因,当我将应用程序发送到后台并且让我的iPhone进入睡眠状态时,onDisconnect会被触发。我实际上希望只有当用户退出或丢失互联网连接时,在线状态才会关闭。我的代码或设置有什么问题?

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?

推荐答案

onDisconnect 事件当客户端与Firebase数据库服务器断开连接时会触发,并且当您的应用程序转到后台时会发生这种情况。 Firebase的观点与开车进入隧道的用户和手机进入睡眠状态之间没有区别。在这两种情况下,客户端和服务器之间的连接都会被删除,因此 onDisconnect()将被触发。

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.

您通常最终会使用 .info / connected onDisconnect()设置上次看到用户的时间值,同时使用 onAuthStateChanged()设置正在登录的用户的状态标志。然后你首先显示已登录的用户,按照他们最近活动的时间顺序显示用户列表。

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.

这篇关于如果应用程序进入后台模式,则会触发Ondisconnect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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