【问题标题】:Alamofire: How to download files sequentiallyAlamofire:如何按顺序下载文件
【发布时间】:2015-08-24 23:37:25
【问题描述】:

我有一系列要下载的视频文件。我正在使用 for 循环来下载它们中的每一个。但是,当循环运行时,所有文件并行下载,这导致应用程序挂起,UI 冻结,CPU 使用率飙升。

for url in urlArray{
  downloadfile(url)
}

我有一个功能可以下载给定 URL 的文件。

func downloadFile(s3Url:String)->Void{ 
    Alamofire.download(.GET, s3Url, destination: destination)
         .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
             println(totalBytesRead)
         }
         .response { request, response, _, error in
             println(response)
         }
}

如何更改此设置以使文件不会同时下载?另外,如何检查下载是否完成,以便更新我的 UI?

【问题讨论】:

  • @mattt 你能提供一个例子吗?我尝试将 downloadFile 包装在 NSBlockOperation 中,但它没有用。即: let operation = NSBlockOperation { () -> Void in println("starting download") self.downloadfile(url!) } queue.addOperation(operation)
  • 这应该就是它的全部了。什么不工作?
  • @mattt 它仍然会同时下载它们。我现在放弃了

标签: ios swift alamofire


【解决方案1】:
func downloadFile(var urlArray:[String])->Void{
    if let s3Url = urlArray.popLast(){
        Alamofire.download(.GET, s3Url, destination: destination)
            .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
                println(totalBytesRead)
            }
            .response { request, response, _, error in
                downloadFile(urlArray)
                println(response)
        }
    }
}

斯威夫特 3.0:

func downloadFile(urlArray:[String])->Void{
    var urlArray = urlArray
    if let s3Url = urlArray.popLast(){
        Alamofire.download(.GET, s3Url, destination: destination)
            .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
                println(totalBytesRead)
            }
            .response { request, response, _, error in
                downloadFile(urlArray)
                println(response)
        }
    }
}

【讨论】:

  • 一个调用自己的函数让我有点“恐惧”,但它按预期工作。 (做成这样,世界不应该爆炸……)
  • 遗憾的是,var 将在 Swift 3 中被删除。我们如何更改这些代码?
  • 你从哪里听说 var 被删除了?
  • 是否同时将一个控制器切换到另一个控制器,我的意思是在后台说?
【解决方案2】:

您可以做的是在文件完成下载时调用另一个函数(即 totalBytesRead >= totalBytesExpectedToRead)。在该函数中,从列表中弹出下一项并使用新 URL 再次调用下载函数。您可以创建一个包含所有 URL 的数组,当您需要一个新项目时,将其从数组中删除并将其传递给下载函数。检查数组是否为空,如果是,则说明您已完成下载所有内容。

你不能只使用循环的原因是 Alamofire 请求总是异步的,所以在发出请求后,控制权立即返回,并且你的程序在下载任何数据之前继续运行。

【讨论】:

    【解决方案3】:

    我希望这会有所帮助。

    var urlArray:[String] =["your file link","your file link"] 
    
    func downloadFile(url: String)->Void{
    
            let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
    
            Alamofire.download(
                url,
                method: .get,
                encoding: JSONEncoding.default,
                to: destination).downloadProgress(closure: { (progress) in
                    //progress closure
    
                }).response(completionHandler: { (DefaultDownloadResponse) in
                    //here you able to access the DefaultDownloadResponse
                    //result closure
                    self.urlArray.removeFirst()
                    if DefaultDownloadResponse.response?.statusCode == 200 {
    
                        print(DefaultDownloadResponse.destinationURL)
    
                        if !self.urlArray.isEmpty{
                                self.downloadFile(url: self.urlArray[0])
                        }
                    }
    
                })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-26
      • 2022-01-13
      • 1970-01-01
      相关资源
      最近更新 更多