ios - firebase setValue 方法中缺少值阻止设置数据库中的其他值

标签 ios swift database authentication setvalue

当用户注册时,Firebase会对用户进行身份验证并上传他的值,例如用户名和电子邮件以及个人资料图片,但如果他跳过个人资料图片,用户将通过身份验证并注册他的电子邮件,但Firebase不会设置用户名和电子邮件,并且关闭个人资料图片

@IBAction func SignUp(_ sender: Any) {
        if  username.text != "", email.text != "", password.text != "", fullname.text != "" {

            activityIndicator.startAnimating()

            Auth.auth().createUser(withEmail: email.text!, password: password.text!, completion: { (user, error) in
                if error != nil {
                    AlertController.showAlert(self, titel: "Error", message:".Fill all fields\n .User do exists\n .Network error ")
                    self.activityIndicator.stopAnimating()
                    return
                }
                let uid = user?.uid
                let storageRef = Storage.storage().reference(forURL: "gs://........com").child("profile_image").child(uid!)
                if let profileImg = self.selectedImage, let imageData = UIImageJPEGRepresentation(profileImg, 0.1){
                    storageRef.putData( imageData, metadata: nil, completion: { (metadata, error) in
                        if error != nil{
                            return
                        }
                        let profileImageUrl = metadata?.downloadURL()?.absoluteString
                        let ref = Database.database().reference()
                        let usersReferance = ref.child("users")
                        let newUserReferance = usersReferance.child(self.username.text!).child(uid!)
                        newUserReferance.setValue(["fullname": self.fullname.text, "username": self.username.text, "email": self.email.text, "password": self.password.text, "profileImageUrl": profileImageUrl])
                    })
                }
                self.dismiss(animated: true, completion: nil)
                self.activityIndicator.stopAnimating()
            })
}
}
}

最佳答案

您必须将值设置为图像检查器闭包之外的不同字段,否则如果 selectedImage 为零,则它们不会设置。

@IBAction func SignUp(_ sender: Any) {
if  username.text != "", email.text != "", password.text != "", fullname.text != "" {

    activityIndicator.startAnimating()

    Auth.auth().createUser(withEmail: email.text!, password: password.text!, completion: { (user, error) in
        if error != nil {
            AlertController.showAlert(self, titel: "Error", message:".Fill all fields\n .User do exists\n .Network error ")
            self.activityIndicator.stopAnimating()
            return
        }
        let uid = user?.uid

        var userValues = [
            "fullname": self.fullname.text,
            "username": self.username.text,
            "email": self.email.text,
            "password": self.password.text
        ]

        let storageRef = Storage.storage().reference(forURL: "gs://........com").child("profile_image").child(uid!)
        if let profileImg = self.selectedImage, let imageData = UIImageJPEGRepresentation(profileImg, 0.1){
            storageRef.putData( imageData, metadata: nil, completion: { (metadata, error) in
                if error != nil{
                    return
                }
                let profileImageUrl = metadata?.downloadURL()?.absoluteString
                userValues["profileImageUrl"] = profileImageUrl

                setData(userValues, uid!)
            })
        } else {
            setData(userValues, uid!)
        }



        self.dismiss(animated: true, completion: nil)
        self.activityIndicator.stopAnimating()
    })
}
}

func setData(_ data: [String: Any], uid: Int) {
    let ref = Database.database().reference()
    let usersReferance = ref.child("users")
    let newUserReferance = usersReferance.child(data["username"]).child(uid)
    newUserReferance.setValue(data)
}

您也应该使用函数来存储数据,因为图像的 putData 是异步的。

关于ios - firebase setValue 方法中缺少值阻止设置数据库中的其他值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47406556/

相关文章:

ios - 我们可以为 iphone 和 mac 应用程序使用相同的 Crashlytics 应用程序吗

iphone - iOS Enterprise OTA 启动,但图标下出现 "Waiting..."

swift - 将不同的按钮动画到正确的位置

ios - 从 Objective-C 调用 Swift 扩展函数的语法

node.js - API 分页、过滤、排序 VS CLIENT 分页、过滤、排序

ios - 如果我误删除了MainStoryboard_iPad,如何制作通用应用程序

ios - 刽子手计划

ios - 将 MapperObject 映射到 Model-swift

mysql - Joomla 3.1 JDatabase 联合查询

database - wm_concat 的来源