如何在设备断开连接之前写入firebase,如果用户与互联网断开连接,我会尝试更新firebase上的值。我不确定是否有方法可以做到这一点。我搜索并找到了onDisconnect
,但这只有在用户注销时才有用。如果有任何建议值得赞赏。
func isUserLoggedInSaved(){
if let user = FIRAuth.auth()?.currentUser {
if !user.isAnonymous {
let path = "rquest/frontEnd/users/\(self.currentUserId()!)"
let connectedRef = FIRDatabase.database().reference(withPath: ".info/connected")
let presenceRef = FIRDatabase.database().reference(withPath: path).child("isUserLogon")
presenceRef.onDisconnectSetValue(false, withCompletionBlock: { (err, fir) in
print("Disconnected")
})
connectedRef.observe(.value, with: { snapshot in
if let connected = snapshot.value as? Bool, connected {
self.childRef(path).updateChildValues(["isUserLogon":true])
print("connectedssss")
return
}else{
print("not connected anymore ")
if #available(iOS 10.0, *) {
Timer.scheduledTimer(withTimeInterval: 3, repeats: false, block: { (timer) in
self.navAlertCustom(viewType: .StatusLine, theme: Theme.warning, duration: 10, location: .top, message: "No Internet Connection", dimBg: false)
})
})
} else {
// Fallback on earlier versions
}
}
})
}
}
}
答案 0 :(得分:0)
查看文档
FIRDatabase.database().reference().onDisconnectSetValue(value: Any)
onDisconnectSetValue
上的cmd +点击 ,您将导航到文档: -
确保在客户端断开连接时(由于关闭浏览器,导航到新页面或网络问题),此位置的数据设置为指定值...