ios - 如果用户与互联网断开连接,Firebase 会更新 Firebase 上的值

标签 ios swift firebase firebase-realtime-database

如何在设备断开连接之前写入 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
                        }
                    }
                })
            }
        }
    }

最佳答案

查看文档

FIRDatabase.database().reference().onDisconnectSetValue(value: Any)

cmd+clickonDisconnectSetValue 上,您将导航到文档:-

Ensure the data at this location is set to the specified value when the client is disconnected (due to closing the browser, navigating to a new page, or network issues)...

关于ios - 如果用户与互联网断开连接,Firebase 会更新 Firebase 上的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43647575/

相关文章:

ios - 快速更新 TabController View

swift - 如何在 TableView 中动态切换 UISwitch?

android - Firebase 身份验证不成功android

ios - swift 非空函数应该在 guard let 中返回一个值

ios - 我们可以将数据从表格单元格传递到都是 xib 文件的表格 View 吗?

ios - 显示空白屏幕的webview屏幕截图

ios - 如何使水平 StackView 具有第一个元素的宽度并填充其余元素

ios - 如何在 Swift 中基本实现委托(delegate)?

firebase - 如何使用 Firebase 身份验证和托管将登录用户重定向到子域?

android - firebase 数据库按值排序工作错误