我正在使用此guide来下载swift 3中的文件。我尝试通过添加某个循环来操作它,以便能够下载n个文件。它有效,但它最多只能达到24 kb。
这是我的代码:
for jsonDict in data{
var jasonData = Attachment()
if let data = (jsonDict["url"] as? String) {jasonData.url = data} else { jasonData.url = ""}
self.beginDownloadFile(jasonData: jasonData)
}
func beginDownloadFile(jasonData: Attachment){
let identifier = jasonData.customer_id + "/attachment/" + jasonData.patient_guid + "@%E%@" + "/" + jasonData.filename + "@%E%@" + jasonData.customer_id + "@%E%@" + jasonData.patient_guid
let backgroundSessionConfiguration = URLSessionConfiguration.background(withIdentifier: identifier)
backgroundSession = Foundation.URLSession(configuration: backgroundSessionConfiguration, delegate: self, delegateQueue: OperationQueue.main)
var link = self.constants.PATIENTS_PICTURE_LINK + jasonData.customer_id + "/attachment/" + jasonData.guid + "/" + jasonData.filename
link = link.replacingOccurrences(of: " ", with: "%20")
let url = URL(string: link)!
downloadTask = backgroundSession.downloadTask(with: url)
downloadTask.resume()
}
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didFinishDownloadingTo location: URL){
let string_container = session.configuration.identifier!.components(separatedBy: "@%E%@")
let folderPath = string_container[0]
let fileName = string_container[1]
let customerID = string_container[2]
let patientGUID = string_container[3]
print("GUID IS: " + patientGUID)
let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let documentDirectoryPath:String = path[0]
let fileManager = FileManager()
let path1 = documentDirectoryPath.stringByAppendingPathComponent(pathComponent: customerID)
let path2 = path1.stringByAppendingPathComponent(pathComponent: "attachment")
let path3 = path2.stringByAppendingPathComponent(pathComponent: patientGUID)
let destinationURLForFile = URL(fileURLWithPath: path3.appendingFormat(fileName))
do {
if !fileManager.fileExists(atPath: path1) {
try fileManager.createDirectory(atPath: path1, withIntermediateDirectories: false, attributes: nil)
}
if !fileManager.fileExists(atPath: path2) {
try fileManager.createDirectory(atPath: path2, withIntermediateDirectories: false, attributes: nil)
}
if !fileManager.fileExists(atPath: path3) {
try fileManager.createDirectory(atPath: path3, withIntermediateDirectories: false, attributes: nil)
}
} catch let error as NSError {
print("ATTACHMENT ERROR: " + error.localizedDescription);
}
if fileManager.fileExists(atPath: destinationURLForFile.path){
//showFileWithPath(path: destinationURLForFile.path)
do {
try fileManager.removeItem(at: destinationURLForFile)
try fileManager.moveItem(at: location, to: destinationURLForFile)
//showFileWithPath(path: destinationURLForFile.path)
}catch{
print("An error occurred while moving/deleting file to destination url")
}
}
else{
do {
try fileManager.moveItem(at: location, to: destinationURLForFile)
//showFileWithPath(path: destinationURLForFile.path)
}catch{
print("An error occurred while moving file to destination url")
}
}
}
结果:
是什么导致它停止@ 24 kb?是因为循环吗?还是标识符操作?
答案 0 :(得分:0)
您声明downloadTask
的代码在哪里?它是一个实例属性吗?
如果是这样,那么每次循环运行时你都会丢弃指向downloadTask
的指针,并且这段代码会被运行:
downloadTask = backgroundSession.downloadTask(with: url)
downloadTask.resume()
由于这个原因,您的任务可能会提前发布。如果是这种情况,则通过循环的最后时间应该完成下载。
另外,24k是否可能是正确的文件大小?或者可能是静态错误页面的大小?