I am using this guide for downloading files in swift 3. I tried to manipulate it by adding a certain loop to be able to download n number of files. It works, yet it only completes up to 24 kb.
Here's my code:
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")
}
}
}
Result:
What causes it to stop @ 24 kb? Is it because of the loop? or the identifier manipulation?
where is the code where you declare downloadTask
? is it an instance property?
if so, then you are throwing away your pointer to downloadTask
each time the loop runs, and this code gets ran:
downloadTask = backgroundSession.downloadTask(with: url)
downloadTask.resume()
it is possible that your task is being released early because of this. if that is the case, then the last time through the loop the download should complete.
also, is it possible that 24k is the correct file size? or maybe the size of a static error page?