如何在Alamofire中暂停/恢复/取消我的下载请求
问题描述:
我正在使用Alamofire下载一个文件并且进度下载,但我不知道如何暂停/恢复/取消特定请求。如何在Alamofire中暂停/恢复/取消我的下载请求
@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
}
答
保持在downloadBtnTapped
与属性创建的请求的引用,在pauseBtnTapped
呼吁cancel
该财产。
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()
}
答
request.cancel()
将取消下载进度。如果你想暂停并继续,你可以使用:
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()
}
+0
暂停和取消有什么区别?暂停更类似于暂停? – 2017-08-11 03:19:38
这是否取消所有请求? – 2015-07-16 09:54:30
'request.cancel()'不保证立即取消请求。这使取消后调用进度块。有没有任何内置的方法来检查取消/挂起是否被调用? – osrl 2016-04-27 10:35:52
暂停是请求?.suspend()或请求?取消()? – Steve 2016-06-27 06:47:00