gpt4 book ai didi

swift - 数据上传到 Firebase Storage 但不更新子节点的值

转载 作者:行者123 更新时间:2023-11-30 10:35:51 25 4
gpt4 key购买 nike

我的问题是,我正在将图像上传到 Firebase 存储,但 URL 未更新到数据库中的子节点,因此我无法引用该图像。

我已检查以确保正确输入数据库值,并且更新子节点似乎可以在该特定功能之外工作。我尝试添加一个调度队列,因为它是一个异步函数,但没有成功。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
var selectedImageFromPicker: UIImage?
if let originalImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
selectedImageFromPicker = originalImage
print("The photo is \(originalImage)")
}
if let selectedImage = selectedImageFromPicker, let cell = selectedCell {
cell.imageContainer.image = selectedImage
print("My image is now \(String(describing: cell.imageContainer.image))")
guard let user = Auth.auth().currentUser else {return}
let uid = user.uid
let imageName = NSUUID().uuidString
let storageRef = Storage.storage().reference().child("\(imageName).png")
if let uploadData = cell.imageContainer.image!.pngData() {
storageRef.putData(uploadData, metadata: nil, completion:
{ (metadata, error) in
if error != nil {
print(error as Any)
return
}
// Function doesn't seem to behave properly after this line
storageRef.downloadURL(completion: { (url, error) in
if let error = error {
print(error.localizedDescription)
return
} else {
let downloadUrl = url?.absoluteString
var valueKey = ""
if cell.activeCell == "0" { valueKey = "imageOne" } else if cell.activeCell == "1" {
valueKey = "imageTwo"} else if cell.activeCell == "2" { valueKey = "imageThree"} else if cell.activeCell == "3" { valueKey = "imageFour" } else if cell.activeCell == "4" { valueKey = "imageFive" } else if cell.activeCell == "5" { valueKey = "imageSix"} else { valueKey = "imageSeven" }

print("the node is titled \(valueKey)")
self.value = [valueKey: downloadUrl] as [String : AnyObject]
print("Here it is \(self.value) for \(uid)")
}
})

self.updateImageData(uid: uid, values: self.value)
print(metadata as Any)
})
}
collectionView.reloadData()
}
dismissView()
}

func updateImageData(uid: String, values: [String: AnyObject]) {
Database.database().reference().child("profile_images").child(uid).updateChildValues(values as [AnyHashable : Any], withCompletionBlock: { (error, ref) in
if let error = error {
print("Failed to update database values with error", error.localizedDescription)
return
}
})
}

预期结果是将 URL 链接视为数据库内相应键的值。

最佳答案

storageRef.downloadURL 方法调用服务器,可能需要一些时间才能完成。您的主代码不会阻止代码并使应用对用户无响应,而是继续执行,并立即调用 self.updateImageData(uid: uid,values: self.value),而无需提供正确的 URL。然后,当服务器返回正确的下载 URL 时,您的完成处理程序将运行并设置 self.value

因此,所有需要下载 URL 的代码都应位于 storageRef.downloadURL 的完成处理程序中。

类似于:

storageRef.downloadURL(completion: { (url, error) in
if let error = error {
print(error.localizedDescription)
return
} else {
let downloadUrl = url?.absoluteString
var valueKey = ""
if cell.activeCell == "0" { valueKey = "imageOne" } else if cell.activeCell == "1" {
valueKey = "imageTwo"} else if cell.activeCell == "2" { valueKey = "imageThree"} else if cell.activeCell == "3" { valueKey = "imageFour" } else if cell.activeCell == "4" { valueKey = "imageFive" } else if cell.activeCell == "5" { valueKey = "imageSix"} else { valueKey = "imageSeven" }

self.value = [valueKey: downloadUrl] as [String : AnyObject]

self.updateImageData(uid: uid, values: self.value)
}
})

关于swift - 数据上传到 Firebase Storage 但不更新子节点的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58054291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com