我正在研究一个项目并试图弄清楚我的下载目的地是否会失败。
例如,如果目标URL或路径因为已存在而无法清除,我希望处理此问题并阻止发出请求。
是否有一种简单的方法可以在目标关闭时停止下载请求?
Alamofire
.download(
.GET,
url,
destination:{
(temporaryURL, response) in
let manager = NSFileManager.defaultManager()
let directoryURL = manager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let pathComponent = response.suggestedFilename
let localPath = directoryURL.URLByAppendingPathComponent(NSBundle.mainBundle().bundleIdentifier ?? "Default", isDirectory:true).URLByAppendingPathComponent(pathComponent!)
if (manager.fileExistsAtPath(localPath!.path!)) {
do {
try manager.removeItemAtURL(localPath!)
} catch let error {
print(error)
==>
HOW CAN I GET THIS TO STOP THE DOWNLOAD
AND CALL RESPONSE WITH AN ERROR
<==
}
}
return localPath!
}
)
.validate(statusCode: 200..<300)
.response {
...
};
作为奖励,我希望能够将此作为错误发送到响应闭包,这将使错误处理更容易。