根据firebase数据库中的值设置开关

时间:2017-10-27 23:59:03

标签: ios swift firebase

我想检查当前用户onlineOfflineStatus的firebase,它将返回" Online"或"离线"然后,当他们登录时,交换机被设置为正确的位置,因为当他们注销时,它应该被设置为离线,这意味着当它中的日志退出时,虽然我无法弄清楚它?

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


    }


}

1 个答案:

答案 0 :(得分:1)

您可以收听连接状态。

根据文件: 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
    }
}