user2834172 user2834172 - 3 days ago 4
iOS Question

why previous finish of download fires, after stop app and running again?

I track downloads using two arrays, to track my downloads and to know where to save them:

private var filesToDownload: NSMutableArray = []
private let startedDownloads: NSMutableArray = []


When a download finishes remove them from both arrays and move the downloaded file to a persistent location.

//is called once the download is complete
internal func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {

print("download task", downloadTask.originalRequest!)
print("download tasks", self.startedDownloads)
// find matching manifestObject through finished downloadTask and use it to generate path

let indexDownloadTask = self.startedDownloads.indexOfObjectIdenticalTo(downloadTask.originalRequest!)
let listItem = self.filesToDownload.objectAtIndex(indexDownloadTask)

// move file
FileSystem.Instance().moveToRoot(location, relativeTo: listItem["file"] as! String)

// remove downloadTask and matching item in filesToDownload to enshure the indexes of both arrays still matches
self.startedDownloads.removeObjectAtIndex(indexDownloadTask)
self.filesToDownload.removeObjectAtIndex(indexDownloadTask)

print("Remaining Downloads1: ", self.startedDownloads.count)
print("Remaining Downloads2: ", self.filesToDownload.count)

// finished all downloads
if self.startedDownloads.count == 0 {
self.working = false
self.cb(result: 0)
print("Crazy shit, we finished downloading all files")
}
}


Now when run my app it starts automatically all downloads. If I let them finish everything is fine. But when i click on stop in Xcode in the middle of the downloads and run it again, it seems to continue with
previous
downloads and will throw out of bound since the
previous
download is not yet in the array of
started downloads
of course.

why do the previous downloads continue?

When using the simulator and clicking stopping and running creates a new folder with device "udid" and a new app "udid" so is it like a fresh install? or continue? if continue shouldn't it use the same folder to continue with the same files?

When a dowload fires finish, i move them from the temp location to a persitent location, but since it has made a new folder for the new click on run, all trys to move will fail anyway. I'm really confused, this seems so senseless.

I start them with

internal func download(url: NSURL) -> Void {
self.working = true
let session = self.getSession()
let task = session.downloadTaskWithURL(url)
self.startedDownloads.addObject(task.originalRequest!)
task.resume()
}


and use this config

private let sessionConfig: NSURLSessionConfiguration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("com.company.app.background")

Answer

If you have your app set up for background downloading then the system continues the download even after the app is terminated. For an app that's launched from the springboard, the system will re-launch your app in the background once the download is completed.

The docs tell you that when you get launched you should recreate your download session object and set up a delegate and you'll get the download complete message.

If you don't want a download started in the last run to be delivered at app launch then you probably don't want background downloading.

Comments