0

I am trying to perform segue after downloading. I tried BlockOperation firstly, but I failed.

Below is my code.

    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) }

I want to set a array of [BlockOperation] firstly.

Then, trying to append Alamofire.download in this array, but failed.

I am not sure which part got wrong, maybe each block need a different name?

Please help me.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Atimis
  • 66
  • 1
  • 11

1 Answers1

0

The tasks do not always terminate in the same order that you pushed them into the queue.

You also must use UI operations on the main thread!

See this post How is it possible to perform multiple Alamofire requests that are finished one after another?

You can also read this blog post for an example of using BlockOperation

Xvolks
  • 2,065
  • 1
  • 21
  • 32