【问题标题】:Firebase setValue on offlineFirebase setValue 离线
【发布时间】:2017-07-22 23:22:42
【问题描述】:

我会在他注册时将用户数据保存到 firebase,但有一个问题是,如果他创建了 Auth 帐户,然后在保存他的信息之前失去了连接,这意味着他将获得没有任何个人资料信息的 Email\Password。

那么问题是如果他失去连接或退出应用程序,如何再次设置值?

FIRAuth.auth()?.createUser(withEmail: self.emailTxt.text!, password: self.passTxt.text!, completion: {
        (user, error) in
        if error != nil {
            if let isError = error?.localizedDescription
            {
                print(isError)
            } else {
                print("Error: 33")
            }
        }
        else if user != nil
        {
            // Email and password created
            let userInfo: [String : Any] = ["timestamp": FIRServerValue.timestamp(),
                                            "uid" : user!.uid,
                                            "email" : self.emailTxt.text!,
              //  This info must be send again if its failed.
                                        ]
              self.ref.child("users").child("profile").child((user?.uid)!).setValue(userInfo) {
                (error, ref) in

                if error != nil {
                    print("error set profile info")
                    // What should i do if its failed? 
                } else {
                    print("done creating user's profile")
                }
        }
    }
    }// Completion end.

【问题讨论】:

    标签: firebase swift3 firebase-realtime-database firebase-authentication


    【解决方案1】:

    您需要启用磁盘持久性

    Database.database().isPersistenceEnabled = true
    

    Firebase 应用会自动处理临时网络中断。缓存数据在离线时可用,Firebase 会在网络连接恢复时重新发送任何写入。

    这里是Firebase Offline Capabilities documentation.

    【讨论】:

    • 我已经在 appDelegate 中写过了,但是当我点击注册然后快速关闭互联网时,只创建了身份验证“电子邮件\密码”,如果我再次连接到互联网,数据库上也没有任何内容!这是我遇到的错误“发生了网络错误(例如超时、连接中断或无法访问的主机)。”
    • 我认为您需要使用 Firebase Cloud Function 来正确执行此操作。 (参见 Frank van Puffelen 的回答)
    【解决方案2】:

    你有两个选择:

    1. 使用磁盘持久化,这意味着当用户重新上线时将写入数据。这就是 Pipiks 的回答所解释的。

    2. 不要依赖客户端创建注册信息,而是使用云函数。

    对于第二个选项,您使用Cloud Functions for Firebase 编写JavaScript code that is executed on Google's servers when the user account is created。请参阅此example of sending a welcome email(您必须修改它才能写入数据)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      • 1970-01-01
      • 1970-01-01
      • 2018-06-23
      • 2015-11-21
      • 1970-01-01
      相关资源
      最近更新 更多