【问题标题】:cancel one Alamofire download request with a specific url取消一个带有特定 url 的 Alamofire 下载请求
【发布时间】:2017-12-24 06:51:18
【问题描述】:

我有一个表格视图,可以为每个单元格下载视频文件。这是我下载视频文件的代码。

 func beginItemDownload(id:String,url:String,selectRow:Int) {

    let pathComponent = "pack\(self.packID)-\(selectRow + 1).mp4"

    let destination: DownloadRequest.DownloadFileDestination = { _, _ in
      let directoryURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
      let folderPath: URL = directoryURL.appendingPathComponent("Downloads", isDirectory: true)
      let fileURL: URL = folderPath.appendingPathComponent(pathComponent)
      return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
    }
    let url = URL(string:url)

    Alamofire.download(
      url!,
      method: .get,
      parameters: nil,
      encoding: JSONEncoding.default,
      headers: nil,
      to: destination).downloadProgress(closure: { (progress) in
        DispatchQueue.main.async {

          if progress.fractionCompleted < 1 {

             print(Float(progress.fractionCompleted))

          }

          if progress.fractionCompleted == 1 {
            print("Completed")
          }
        }

      }).response(completionHandler: { (defaultDownloadResponse) in

        if let destinationUrl = defaultDownloadResponse.destinationURL  {
          DispatchQueue.main.async {

                    print("destination url -****",destinationUrl.absoluteString)

          }
        }
        if let error = defaultDownloadResponse.error {
          debugPrint("Download Failed with error - \(error)")         
          return
        }
      })
  }

点击每个表格视图单元格的下载按钮时,我可以下载视频文件并为每个单元格分配进度值。但是现在我想在点击每个单元格中的取消按钮时取消此单元格的下载请求,. 我为此问题搜索了不同的解决方案,但我找不到任何解决方案来取消具有特定 url 字符串的一个请求。怎么能解决这个问题。 谢谢大家的回复。

【问题讨论】:

    标签: ios swift alamofire


    【解决方案1】:

    未经测试但应该可以使用,使用originalRequest(这是创建任务时的原始请求)或可选的currentRequest(这是当前正在处理的请求)来定位您要取消的特定任务:

    func cancelSpecificTask(byUrl url:URL) {
        Alamofire.SessionManager.default.session.getAllTasks{sessionTasks in
            for task in sessionTasks {
                if task.originalRequest?.url == url {
                    task.cancel()
                }
            }
    
        }
    }
    

    或者只取消下载任务:

    func cancelSepcificDownloadTask(byUrl url:URL) {
        let sessionManager = Alamofire.SessionManager.default 
        sessionManager.session.getTasksWithCompletionHandler { dataTasks, uploadTasks, downloadTasks in 
        for task in downloadTasks {
                if task.originalRequest?.url == url {
                task.cancel()
            }
        }
    }
    

    【讨论】:

    • 所有请求的请求 url 相同,参数不同!是用参数或任何其他方式检查请求的任何方式
    【解决方案2】:

    虽然luiyezheng's answer 非常好并且应该可以完成这项工作,但开发人员(例如我)有时会忽略Alamofire.download() 实际上会返回DownloadRequest,如果需要可以将其存储在某个地方,然后再存储cancel()`ed :

    let request = Alamofire.download("http://test.com/file.png")
    r.cancel()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      • 2015-05-09
      • 2014-12-05
      • 1970-01-01
      • 2016-12-29
      相关资源
      最近更新 更多