我想更改用户个人资料图片,但我的所有功能都是删除实时数据库中的 child("profileImageUrl")
。新的个人资料图片按预期上传到存储,但孩子没有被更新而是被删除。我还没有找到解决方案,有人可以帮我吗?
这是我的功能:
func updateProfileImage() {
guard imageChanged == true else { return }
guard let currentUid = Auth.auth().currentUser?.uid else { return }
guard let user = self.user else { return }
Storage.storage().reference(forURL: user.profileImageUrl).delete(completion: nil)
let filename = NSUUID().uuidString
guard let updatedProfileImage = profileImageView.image else { return }
guard let imageData = updatedProfileImage.jpegData(compressionQuality: 0.3) else { return }
STORAGE_PROFILE_IMAGES_REF.child(filename).putData(imageData, metadata: nil) { (metadata, error) in
if let error = error {
print("Failed to upload image to storage with error: ", error.localizedDescription)
}
STORAGE_PROFILE_IMAGES_REF.downloadURL(completion: { (url, error) in
USER_REF.child(currentUid).child("profileImageUrl").setValue(url?.absoluteString, withCompletionBlock: { (err, ref) in
guard let userProfileController = self.userProfileController else { return }
userProfileController.fetchCurrentUserData()
self.dismiss(animated: true, completion: nil)
})
})
}
}
我像这样存储用户数据:
let dictionaryValues = ["name": fullname,
"username": username,
"profileImageUrl": profileImageUrl]
let values = [uid: dictionaryValues]
Database.database().reference().child("users").updateChildValues(values)