0

I'm trying to upload multiple images to firebase storage, and the app needs to do something after the uploading tasks finish. I did some research, and I found barrier is useful in this case. I followed the tutorial, but it seems my code is not working properly. What did I miss? Here is the code:

func uploadImages(completion: (()->Void)){
    // creating a queue
    let uploadImagesQueue = DispatchQueue(label: "upLoadImages", attributes: .concurrent)
    for i in 0..<numOfImages{
        uploadImagesQueue.async{
            // upload each of the images
        }
    }
    uploadImagesQueue.async(flags: .barrier){
        completion()
    }
}

The problem of this code is that completion() runs before all the upload parts finish. How can I fix this?

Michael Bleigh
  • 25,334
  • 2
  • 79
  • 85
AlexBains
  • 295
  • 6
  • 14

1 Answers1

0

Maybe inside the uploadImagesQueue.async block, you uploaded the images asynchronously(by using uploadTask!?). Then the async block finished at the time the uploadTask is submitted. Instead of using DispatchQueue, just use Firebase's uploadTask.

Descartes
  • 59
  • 2