Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何从不同的类更新Firebase数据库中的值?_Swift_Firebase_Firebase Realtime Database_Swift3 - Fatal编程技术网

Swift 如何从不同的类更新Firebase数据库中的值?

Swift 如何从不同的类更新Firebase数据库中的值?,swift,firebase,firebase-realtime-database,swift3,Swift,Firebase,Firebase Realtime Database,Swift3,我有一个注册类,它有三个文本字段,分别是用户名、电子邮件和密码。当用户点击注册按钮时,将调用handleRegister()函数。此函数从这些textfields.text中获取三个值,并将它们发送到子节点下的my Firebase数据库,其中包含它们的用户id,如下图所示: 我的问题是,我希望能够在注册类之外更新这些值中的任意3个(电子邮件、名称、密码)。我如何做到这一点?非常感谢。这是我的注册码: func handleRegister() { guard let email =

我有一个注册类,它有三个文本字段,分别是用户名、电子邮件和密码。当用户点击注册按钮时,将调用
handleRegister()
函数。此函数从这些textfields.text中获取三个值,并将它们发送到子节点下的my Firebase数据库,其中包含它们的用户id,如下图所示:

我的问题是,我希望能够在注册类之外更新这些值中的任意3个(电子邮件、名称、密码)。我如何做到这一点?非常感谢。这是我的注册码:

func handleRegister() {

    guard let email = emailTextField.text, let password = passwordTextField.text, let name = usernameTextField.text else {
        return
    }

    FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user: FIRUser?, error) in

        if error != nil {
            return
        }

        guard let uid = user?.uid else {
            return
        }

        //successfully registered user.

        let imageName = NSUUID().uuidString

        let storageRef = FIRStorage.storage().reference().child("profile_images").child("\(imageName).png")

        if let uploadData = UIImagePNGRepresentation(self.profileImageView.image!) {
            storageRef.put(uploadData, metadata: nil, completion: { (metadata, error) in

                if error != nil {
                    return
                }

                if let profileImageUrl = metadata?.downloadURL()?.absoluteString {
                    let values = ["name": name, "email": email, "password": password, "profileImageUrl": profileImageUrl]

                    self.registerUserIntoDatabaseWithUID(uid: uid, values: values as [String : AnyObject])
                }
            })
        }
    })
}

private func registerUserIntoDatabaseWithUID(uid: String, values: [String: AnyObject]) {

    let ref = FIRDatabase.database().reference()

    let usersReference = ref.child("users").child(uid)

    usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in

        if err != nil {
            return
        }
        print("Successfully saved user to database.")

        self.dismiss(animated: true, completion: nil)
    })
}
您有两个选择:

选项1:您需要将用户的数据库ID保存在某个位置,以便稍后在应用程序中使用它来处理类似的情况。您可以将ID保存在Userdefaults或其他更安全的地方

选项2:您可以使用Auth.Auth()?.currentUser?.uid检索登录用户的ID

guard let uid = Auth.auth()?.currentUser?.uid else { return }
当您拥有此ID时,您可以像在
RegisterUserOnToDatabaseWithUID()中一样更新数据库中的值

Nota Bene

如果您对此答案有任何疑问,请添加评论。 这个答案和另一个答案的区别在于我是 指示如何根据请求在多个位置更新

您应该考虑使用数据扇出方法。它处理在多个位置写入数据。下面是一个快速代码示例:

let key = ref.child("posts").childByAutoId().key
let post = ["uid": userID,
            "author": username,
            "title": title,
            "body": body]
let childUpdates = ["/posts/\(key)": post,
                    "/user-posts/\(userID)/\(key)/": post]
ref.updateChildValues(childUpdates)
有关此方法的更多信息,请参阅以下文档:

我想通过电子邮件发送id,而不是孩子的帖子。但Firebase不允许在对象中抛出错误无效密钥。键必须为非空且不能包含“.”#“$”[”或“]”
let key = ref.child("posts").childByAutoId().key
let post = ["uid": userID,
            "author": username,
            "title": title,
            "body": body]
let childUpdates = ["/posts/\(key)": post,
                    "/user-posts/\(userID)/\(key)/": post]
ref.updateChildValues(childUpdates)