我正在使用Alamofire将文件下载到我的iOS应用程序中,它适用于较小的文件(约1GB或更少),但是当我尝试下载更大的文件时,它会失败。它完成整个下载,达到100%,然后抛出错误:
FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputFileReadFailed(file:///...))
为什么大文件失败?
这是我的代码:
let destination: (URL, HTTPURLResponse) -> (URL, DownloadRequest.DownloadOptions) = {
(temporaryURL, response) in
if let directoryURL = FileManager().containerURL(forSecurityApplicationGroupIdentifier: applicationGroup), let suggestedFilename = response.suggestedFilename {
let filePath = directoryURL.appendingPathComponent("\(suggestedFilename)")
return (filePath, [.removePreviousFile, .createIntermediateDirectories])
}
return (temporaryURL, [.removePreviousFile, .createIntermediateDirectories])
}
if self.reachability.isReachableViaWiFi {
var request: DownloadRequest
if let resumeData = resumeData {
request = BackendAPIManager.sharedInstance.alamoFireManager.download(resumingWith: resumeData)
.downloadProgress { progress in
cell?.progressView.isHidden = false
cell?.progressView.setProgress(Float(progress.fractionCompleted), animated: true)
}
} else {
request = BackendAPIManager.sharedInstance.alamoFireManager.download(fileLocation, to: destination)
.downloadProgress { progress in
cell?.progressView.isHidden = false
cell?.progressView.setProgress(Float(progress.fractionCompleted), animated: true)
}
}
request.responseData { response in
switch response.result {
case .success( _):
cell?.pctLabel.text = nil
cell?.progressView.isHidden = true
break;
case .failure:
print(response)
resumeData = response.resumeData
}
}
}
class BackendAPIManager: NSObject {
static let sharedInstance = BackendAPIManager()
var alamoFireManager : Alamofire.SessionManager!
var backgroundCompletionHandler: (() -> Void)? {
get {
return alamoFireManager?.backgroundCompletionHandler
}
set {
alamoFireManager?.backgroundCompletionHandler = newValue
}
}
fileprivate override init()
{
let configuration = URLSessionConfiguration.background(withIdentifier: "com.nw.fileDownload.background")
configuration.timeoutIntervalForRequest = 14400 // 4 hours
configuration.timeoutIntervalForResource = 14400
self.alamoFireManager = Alamofire.SessionManager(configuration: configuration)
}
}