我想为.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)
}
}
答案 0 :(得分:1)
呼叫setValue
将替换该位置中的所有现有数据。如果只想更新特定的子节点/属性,请调用updateChildValues
:
newUserReference.updateChildValues(["photoUrl": photoUrl]) { (error, ref) in
...
}
答案 1 :(得分:0)
您正在更改其父节点的值。尝试这样设置:
Database.database().reference().ref("users/\(uid)/photoUrl").setValue(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)
}
}
通过设置photoUrl
的值,您将更改photoUrl
节点而不是userID
节点本身的值。
第一次,因为那里没有photoUrl
节点,Firebase会自动创建一个新的photoUrl
节点并设置其值。