在继续功能之前等待图像上传 - Swift 5 Firebase 存储

Wait for image to be uploaded before continuing function - Swift 5 Firebase Storage

我正在尝试使用 Firebase 存储上传图片。我遇到的问题是,当我调用我的图片上传功能时,上传任务非常慢,最终 运行 最后在我的主要功能中实际被进一步调用时。

有没有办法等待 storageRef.putData 完成后再继续其余的功能?

将压缩质量从 1 降低是否会更快?

这是我的函数:

func newImageUrl(){
    let key = self.itemId
    guard let userID = Auth.auth().currentUser?.uid else { return }
    let storageRef = Storage.storage().reference().child(userID).child("SubCategory").child(self.itemTitle!).child(key!).child("ItemImages.jpg")

    guard let imageData = self.itemImage.image!.jpegData(compressionQuality: 1) else { return }

    print("starting image upload!")
    storageRef.putData(imageData, metadata: nil) { (metadata, error) in
        guard let metadata = metadata else {
            return
        }

        storageRef.downloadURL { (url, error) in
            guard let urlStr = url else{
                return
            }
                let urlFinal = (urlStr.absoluteString)
                self.imageFinalUrl = urlFinal
        }
    }
}

更新功能 --

func newImageUrl(completion:@escaping((String?) -> () )) {
let key = self.itemId
guard let userID = Auth.auth().currentUser?.uid else { completion(nil) ; return }
let storageRef = Storage.storage().reference().child(userID).child("SubCategory").child(self.itemTitle!).child(key!).child("ItemImages.jpg")

guard let imageData = self.itemImage.image!.jpegData(compressionQuality: 1) else { completion(nil) ; return }

print("starting image upload!")
storageRef.putData(imageData, metadata: nil) { (metadata, error) in
    guard let metadata = metadata else {
        return
    }

    storageRef.downloadURL { (url, error) in
        guard let urlStr = url else{
            completion(nil)
            return
        }
        let urlFinal = (urlStr.absoluteString)
        self.imageFinalUrl = urlFinal
        completion(urlFinal)
        }
    }
}

示例主要功能:

func updateItemDetail(){
    print("this is running 1")
    self.newImageUrl { (str) in
        print(str)
    }
    print("this is running 2")

控制台打印:

this is running 1
starting image upload!
this is running 2
Optional("https://firebasestorage.googleapis.com/v0/b/.....etc")

你需要完成

func newImageUrl(completion:@escaping((String?) -> () )) {
    let key = self.itemId
    guard let userID = Auth.auth().currentUser?.uid else { completion(nil) ; return }
    let storageRef = Storage.storage().reference().child(userID).child("SubCategory").child(self.itemTitle!).child(key!).child("ItemImages.jpg")

    guard let imageData = self.itemImage.image!.jpegData(compressionQuality: 1) else { completion(nil) ; return }

    print("starting image upload!")
    storageRef.putData(imageData, metadata: nil) { (metadata, error) in
        guard let metadata = metadata else {
            return
        }

        storageRef.downloadURL { (url, error) in
            guard let urlStr = url else{
                completion(nil)
                return
            }
            let urlFinal = (urlStr.absoluteString)
            self.imageFinalUrl = urlFinal
            completion(urlFinal)
        }
    }
}

通话

newImageUrl { (str) in
  print(str)
  // do your next work here
}