【发布时间】:2017-01-10 02:24:31
【问题描述】:
我正在使用这个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 时停止?是因为循环吗?还是标识符操作?
【问题讨论】:
标签: ios swift nsurlsession nsurlsessiondownloadtask