Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pause/resume/cancel my download request in Alamofire

I am downloading a file using Alamofire download with progress but i have no idea how to pause / resume / cancel the specific request.

@IBAction func downloadBtnTapped() {

 Alamofire.download(.GET, "http://httpbin.org/stream/100", destination: destination)
     .progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) in
         println(totalBytesRead)
     }
     .response { (request, response, _, error) in
         println(response)
     }
}


@IBAction func pauseBtnTapped(sender : UIButton) {        
    // i would like to pause/cancel my download request here
}
like image 590
Zeeshan Avatar asked Oct 10 '14 18:10

Zeeshan


2 Answers

Keep a reference to the request created in downloadBtnTapped with a property, and call cancel on that property in pauseBtnTapped.

var request: Alamofire.Request?

@IBAction func downloadBtnTapped() {
 self.request = Alamofire.download(.GET, "http://httpbin.org/stream/100", destination: destination)
}

@IBAction func pauseBtnTapped(sender : UIButton) {
  self.request?.cancel()
}
like image 99
mattt Avatar answered Sep 23 '22 22:09

mattt


request.cancel() will cancel the download progress. If you want to pause and continue, you can use:

var request: Alamofire.Request?

@IBAction func downloadBtnTapped() {
 self.request = Alamofire.download(.GET, "http://yourdownloadlink.com", destination: destination)
}

@IBAction func pauseBtnTapped(sender : UIButton) {
  self.request?.suspend()
}

@IBAction func continueBtnTapped(sender : UIButton) {
  self.request?.resume()
}

@IBAction func cancelBtnTapped(sender : UIButton) {
  self.request?.cancel()
}
like image 37
marmaralone Avatar answered Sep 21 '22 22:09

marmaralone