如何在 swift3 中完成 5 个文件下载请求后执行 segue

How to perform segue after 5 files download request completed in swift3

我正在尝试在下载后执行 segue。 我先尝试了BlockOperation,但是我失败了。

下面是我的代码。

    let operationQ = OperationQueue()

    let taskArray: [BlockOperation]

    for index in 0..<songsList.count {

        let eachSong = songsList[index]

        let destination: DownloadRequest.DownloadFileDestination = { _, _ in
            let documentsURL = NSHomeDirectory() + "/Documents/"
            let fileURL = URL(fileURLWithPath: documentsURL.appending("song\(index).m4a"))
            print("song\(index).m4a is downloading")

            return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
        }

        taskArray.append(BlockOperation(block: let task = {
            Alamofire.download(eachSong, to: destination).response { _ in

                //                    print(response.response)
            }

            }))

taskArray[4].completionBlock = { performSegue(withIdentifier: "NextVC", sender: self) }

我想先设置一个[BlockOperation]数组

然后,尝试在此数组中附加 Alamofire.download,但失败了。

我不确定哪一部分出错了,也许每个块需要不同的名称?

请帮帮我。

任务并不总是按照您将它们推入队列的相同顺序终止。

您还必须在主线程上使用 UI 操作!

看到这个post

您还可以阅读 this blog post 以获取使用 BlockOperation

的示例