数据上传到 Firebase 存储但不更新子节点的值

Data uploads to Firebase Storage but Doesn't updateValue of child node

我的问题是我正在将图像上传到 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 link 作为数据库中相应键的值。

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)
    }
})