简体   繁体   中英

Implement barrier in swift3

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM