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

标签 ios swift segue grand-central-dispatch

我正在尝试下载后执行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 追加到该数组中,但失败了。

我不确定哪一部分出了问题,也许每个 block 都需要不同的名称?

请帮助我。

最佳答案

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

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

查看这篇文章How is it possible to perform multiple Alamofire requests that are finished one after another?

您还可以阅读this blog post有关使用 BlockOperation

的示例

关于ios - 如何在swift3中完成5个文件下载请求后执行segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45651518/

相关文章:

ios - 在 ViewDidLoad 中 Segue 值为 nil 但在 didSet 中具有值

iphone - UIWebView 切换 scalespagetofit

ios - 从现有的 Xcode 项目制作 CocoaPod

ios - Facebook Analytics 用户事件计数错误

ios - 如何从后台任务添加和重绘 CALayer?

ios - 将数据从 FirstViewController 传递到 LastViewController

ios - '#selector' 的参数未引用初始化程序或方法

swift - 确定 WatchOS 2 应用程序何时打开的最佳方法是什么?

ios - 使用 Parse 刷新数据

swift - Swift 中没有带有标识符错误的 Segue?