ios - urlsession如何确定何时下载所有文件
问题描述
我正在使用 urlsession 从服务器下载一些文件,每次下载后都会触发委托“didFinishDownloadingTo”。但我想要在所有下载完成后触发的东西。
是我必须使用的委托“didCompleteWithError”吗?
我如何知道是否所有文件都已下载?
func downloadPdf() {
for k in self.resultAddressServer {
let fileURL = URL(string: k)
let sessionConfig = URLSessionConfiguration.default
let operationQueue = OperationQueue()
let urlSession = URLSession(configuration: sessionConfig, delegate: self, delegateQueue: operationQueue)
let request = URLRequest(url:fileURL!)
let downloadTask = urlSession.downloadTask(with: request)
downloadTask.resume()
}
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
do {
let manager = FileManager.default
let destinationURL = try manager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
.appendingPathComponent(downloadTask.originalRequest!.url!.lastPathComponent)
try? manager.removeItem(at: destinationURL)
try manager.moveItem(at: location, to: destinationURL)
print(destinationURL)
} catch {
print(error)
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if error != nil {
DispatchQueue.main.async() {
self.statusLabel.text = "Download failed"
}
} else {
DispatchQueue.main.async() {
self.statusLabel.text = "Download finished"
}
}
解决方案
根据其他帖子的建议,我添加了一个数组,在其中添加了 downloadTask 标识符号,然后在任务完成时在委托中删除该标识符 #。最后要知道是否所有下载完成,我只检查array.count何时等于0。
func downloadPdf() {
for k in self.resultAddressServer {
let fileURL = URL(string: k)
let sessionConfig = URLSessionConfiguration.background(withIdentifier: "com.Test")
let urlSession = URLSession(configuration: sessionConfig, delegate: self, delegateQueue: OperationQueue())
let request = URLRequest(url:fileURL!)
let downloadTask = urlSession.downloadTask(with: request)
self.tasksArray.append(downloadTask.taskIdentifier)
print(downloadTask.taskIdentifier)
downloadTask.resume()
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
do {
let manager = FileManager.default
let destinationURL = try manager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
.appendingPathComponent(downloadTask.originalRequest!.url!.lastPathComponent)
try? manager.removeItem(at: destinationURL)
try manager.moveItem(at: location, to: destinationURL)
print(destinationURL)
} catch {
print(error)
}
if let index = self.tasksArray.index(of: downloadTask.taskIdentifier) {
self.tasksArray.remove(at: index)
print(self.tasksArray.count)
}
DispatchQueue.main.async() {
if self.tasksArray.count == 0 {
//Do whatever you want, all downloads are completed
//the DispatchQueue is for what I use this for...maybe not needed in you case
}
}
}
推荐阅读
- javacard - 将 Java 卡与 Bitlocker 一起使用
- php - php 使用 0 键作为 URL 中的值来检索数据
- javascript - 如何将回调函数从 React 传递给 litelement?
- sql - 创建表格视图,汇总行中的所有列以及行名等于列名的列的所有行
- javascript - 允许在使用 jQuery 循环时切换项目
- jquery - jQuery 是否有通配符结尾
- javascript - 从 JSON 输出中获取字符串
- c# - 为什么我不能通过代码将文件插入 SharePoint 列表?它进入异常,似乎访问被拒绝
- python - Keras 相当于 Caffe 的“cpu_data()”方法?
- sharepoint - 如何隐藏仅对管理员有用的行,以便用户不会更改列表视图?