我想为 .child("users")
创建一个新节点,但很难上传它。重要的是,在注册的第一步中,用户使用 email
、username
和 password
进行注册。 email
和 username
成功发送到 .child("users")
。之后,用户将被发送到一个新的 ViewController
,他们应该在其中选择个人资料图片。这是棘手的事情。照片网址确实保存但删除以前保存的数据只留下照片网址。
这是整个过程中发生的事情
1.) user3 is successfully added
2.) Photo url is successfully added, and user and email in process of being deleted
这是我的代码。非常感谢!
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
...
}
关于swift - 我想在 Firebase 中向子节点添加一个节点,但是当我添加值时,它会删除之前发送到数据库的所有其他数据 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51550697/