gpt4 book ai didi

ios - 根据在 firebase 数据库中找到的值设置开关

转载 作者:行者123 更新时间:2023-11-28 06:10:08 25 4
gpt4 key购买 nike

我想检查当前用户 onlineOfflineStatus 的 firebase,它将返回“在线”或“离线”,然后当他们登录时,开关设置到正确的位置,就像他们注销时一样设置为离线意味着当重新登录时离线,虽然我不太明白?

let switchControl: UISwitch


func setLeftNavButton() {

var switchControl=UISwitch()
switchControl = self.switchControl
switchControl.isOn = true
switchControl.setOn(true, animated: false)
switchControl.addTarget(self, action: #selector(switchValueDidChange), for: .valueChanged)
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: switchControl)

}



func setSwitch(){

let online = "Online"
let offline = "offline"

if currentUser?.onlineOfflineStatus == online {

self.switchControl.isOn


} else {

if currentUser?.onlineOfflineStatus == offline

self.switchControl.offline


}


}

最佳答案

您可以监听连接状态。

根据文档: https://firebase.google.com/docs/database/ios/offline-capabilities#section-connection-state

let connectedRef = Database.database().reference(withPath: ".info/connected")
connectedRef.observe(.value, with: { snapshot in
if snapshot.value as? Bool ?? false {
UserDefaults.standard.set(true, forKey: "current_user_is_connected")
} else {
UserDefaults.standard.set(false, forKey: "current_user_is_connected")
}
})

更新您的代码:

func setSwitch(){

if UserDefaults.standard.bool(forKey: "current_user_is_connected") {
self.switchControl.isOn = true
} else {
self.switchControl.isOn = false
}
}

关于ios - 根据在 firebase 数据库中找到的值设置开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46984933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com