【发布时间】:2019-08-13 17:24:42
【问题描述】:
我很难理解我认为什么是容易的。
我有一个 URLSession.downloadTask。我已将下载对象设置为 URLSession 委托,并且以下委托方法确实接收调用,所以我知道我的委托设置正确。
func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?)
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL)
我无法捕获的情况是 downloadTask 填满了 iPad 上的磁盘空间。这些委托方法都不会被调用。
我应该如何发现这个错误?
这是我的下载对象:
import Foundation
import Zip
import SwiftyUserDefaults
extension DownloadArchiveTask: URLSessionDownloadDelegate {
// Updates progress info
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask,
didWriteData bytesWritten: Int64, totalBytesWritten: Int64,
totalBytesExpectedToWrite: Int64) {
let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
self.delegate?.updateProgress(param: progress)
}
// Stores downloaded file
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
print("DownloadArchiveTask: In didFinishDownloadingTo")
}
}
extension DownloadArchiveTask: URLSessionTaskDelegate {
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
print("DownloadArchiveTask: In didCompleteWithError")
if error != nil {
print("DownloadArchiveTask: has error")
self.delegate?.hasDiskSpaceIssue()
}
}
}
extension DownloadArchiveTask: URLSessionDelegate {
// Background task handling
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
print("DownloadArchiveTask: In handler for downloading archive")
DispatchQueue.main.async {
let sessionIdentifier = session.configuration.identifier
if let sessionId = sessionIdentifier, let app = UIApplication.shared.delegate as? AppDelegate, let handler = app.completionHandlers.removeValue(forKey: sessionId) {
handler()
}
}
}
func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
print("DownloadArchiveTask: didBecomeInvalidWithError")
if error != nil {
print("DownloadArchiveTask: has error")
self.delegate?.hasDiskSpaceIssue()
}
}
}
class DownloadArchiveTask: NSObject {
var delegate: UIProgressDelegate?
var archiveUrl:String = "http://someurl.com/archive.zip"
var task: URLSessionDownloadTask?
static var shared = DownloadArchiveTask()
// Create downloadsSession here, to set self as delegate
lazy var session: URLSession = {
let configuration = URLSessionConfiguration.background(withIdentifier: "\(Bundle.main.bundleIdentifier!).background_archive")
return URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
}()
func initialDownload() {
// Create URL to the source file you want to download
let fileURL = URL(string: archiveUrl)
let request = URLRequest(url:fileURL!)
self.task = self.session.downloadTask(with: request)
task?.resume()
}
}
以前有人做过吗?我不敢相信这很难——我一定是以错误的方式解决问题......
【问题讨论】:
-
如果这是一个问题,也许你可以在下载之前检查磁盘空间,或者你应该使用数据任务而不是下载任务,这样你就可以在下载过程中检查磁盘空间.然后,您当然必须自己保存数据(空间允许)。不过,这听起来一定是一个异常大的下载,所以也许您应该解释一下您要做什么。
-
我认为你永远不会遇到这个问题,因为苹果管理内存很好,苹果会删除临时文件然后缓存文件。
-
我会检查数据任务。下载是一个压缩文件,其中包含应用程序的初始图像内容。随着内容随着时间的推移而增长,初始 zip 文件的大小也会随之增长。我们遇到过无法下载 zip 文件的情况。我想要做的是抓住这种情况并将应用程序转换为备份“在线”模式。它适用于网络连接非常差的医院,因此首选的解决方案是将所有图像资产下载并存储在设备上。
标签: ios swift nsurlsessiondownloadtask urlsession