gpt4 book ai didi

ios - 如果此文件已存在,则取消 Alamofire 文件下载

转载 作者:行者123 更新时间:2023-11-28 06:57:58 25 4
gpt4 key购买 nike

如果下载的文件已经存在于文档文件夹中,如何取消 Alamofire 请求?

请求代码如下:

Alamofire.download(.GET, fileUrls[button.tag], destination: { (temporaryURL, response) in
if let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as? NSURL {
let fileURL = directoryURL.URLByAppendingPathComponent(response.suggestedFilename!)
self.localFilePaths[button.tag] = fileURL
if NSFileManager.defaultManager().fileExistsAtPath(fileURL.path!) {
NSFileManager.defaultManager().removeItemAtPath(fileURL.path!, error: nil)
}
return fileURL
}
println("temporaryURL - \(temporaryURL)")
self.localFilePaths[button.tag] = temporaryURL
return temporaryURL
}).progress { _, totalBytesRead, totalBytesExpectedToRead in
println("\(totalBytesRead) - \(totalBytesExpectedToRead)")
dispatch_async(dispatch_get_main_queue()) {
self.progressBar.setProgress(Float(totalBytesRead) / Float(totalBytesExpectedToRead), animated: true)

if totalBytesRead == totalBytesExpectedToRead {
self.progressBar.hidden = true
self.progressBar.setProgress(0, animated: false)
}
}
}.response { (_, _, data, error) in
let previewQL = QLReaderViewController()
previewQL.dataSource = self
previewQL.currentPreviewItemIndex = button.tag
self.navigationController?.pushViewController(previewQL, animated: true)
}

我还尝试创建一个请求变量 var request: Alamofire.Request? 然后取消 request?.cancel() 如果该文件存在但它不起作用。

有人可以帮我解决这个问题吗?

最佳答案

IMO 你不应该首先取消请求而不是取消请求。在启动 Alamofire 请求之前,您应该进行文件检查。

如果您确实觉得需要开始请求,您随时可以在开始请求后立即取消。

var shouldCancel = false

let request = Alamofire.request(.GET, "some_url") { _, _ in
shouldCancel = true
}
.progress { _, _, _ in
// todo...
}
.response { _, _, _ in
// todo...
}

if shouldCancel {
request.cancel()
}

关于ios - 如果此文件已存在,则取消 Alamofire 文件下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32841455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com