swift - 我想在 Firebase 中向子节点添加一个节点,但是当我添加值时,它会删除之前发送到数据库的所有其他数据 - Swift

标签 swift firebase firebase-realtime-database

我想为 .child("users") 创建一个新节点,但很难上传它。重要的是,在注册的第一步中,用户使用 emailusernamepassword 进行注册。 emailusername 成功发送到 .child("users")。之后,用户将被发送到一个新的 ViewController,他们应该在其中选择个人资料图片。这是棘手的事情。照片网址确实保存删除以前保存的数据只留下照片网址。

这是整个过程中发生的事情

1.) user3 is successfully added

2.) Photo url is successfully added, and user and email in process of being deleted

3.) Only phot url remains

这是我的代码。非常感谢!

 var selectedProfileImage : UIImage!

@IBAction func doneButton(_ sender: UIButton) {

            ProgressHUD.show("En Proceso...")
            let storage = Storage.storage()
            let storageRef = storage.reference()
            //photo idstring gives us a unique string for any given time of the day. Garantied unique string
            let photoIdString = "\(NSUUID().uuidString).jpg"
            let imageReference = storageRef.child("profileImages").child(photoIdString)
            if let imageData = UIImageJPEGRepresentation(selectedProfileImage, 0.7) {
                imageReference.putData(imageData).observe(.success) { (snapshot) in
                    imageReference.downloadURL(completion: { (url, error) in

                        if let downloadUrl = url {
                            let directoryURL : NSURL = downloadUrl as NSURL
                            let urlString:String = directoryURL.absoluteString!
                            self.sendDataToUserDatabase(photoUrl: urlString)
                                ProgressHUD.showSuccess("Tu imagen ha sido publicada!")
                                self.performSegue(withIdentifier: "toHomeView", sender: self)
                        }
                        else {
                            print("couldn't get profile image url")
                            ProgressHUD.showError("No hemos podido subir tu foto de perfil. Intenta más tarde")
                            return
                        }
                    })
                }
        }

    }

    //FUNCS

    func sendDataToUserDatabase(photoUrl: String) {
        //por the time being photoUrl:String - wasn't added because I can't get the downloadURL
        var ref: DatabaseReference!
        ref = Database.database().reference()
        let usersReference = ref.child("users")
        let uid = Auth.auth().currentUser?.uid
        let newUserReference = usersReference.child(uid!)
        newUserReference.setValue(["photoUrl": photoUrl]) { (error, ref) in
            if error != nil {
                ProgressHUD.showError(error!.localizedDescription)
                return
            }
            ProgressHUD.showSuccess("Tu imagen ha sido publicada!")
            self.performSegue(withIdentifier: "toHomeView", sender: self)
        }
    }

最佳答案

调用 setValue 替换该位置的所有现有数据。如果您只想更新特定的子节点/属性,请调用 updateChildValues:

newUserReference.updateChildValues(["photoUrl": photoUrl]) { (error, ref) in
    ...
}

另见 Firebase documentation on updating specific children .

关于swift - 我想在 Firebase 中向子节点添加一个节点,但是当我添加值时,它会删除之前发送到数据库的所有其他数据 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51550697/

相关文章:

ios - 更改 tableview 的内容插图

ios - 设置和删除UIButton文本标签

swift - 从 Firebase 获取特定的 .childrenCount

android - Firebase 云消息传递 - Android : click-action of notification when app is closed doesn't work

android - 火力地堡数据库 : How to set default value (Timestamp) to field

ios - 未识别的 CFRunLoopRun 崩溃报告问题

firebase 更复杂的验证

java - 如何仅在 Firebase 数据库发生更改时调用通知?

android - 从 firebase 数据库读取值时超时?

ios - 如何在 Swift 中的滑出菜单的主 UIView 后面添加 UIView?