URLSession downloadTask(with:) creates multiple file downloads - ios

Background: I have a page where users can go to download various map files. I currently have a solution setup but it has some issues. Here is what I have:
public class FileDownloader: NSObject, URLSessionDownloadDelegate {
public let identifier: String
public let progress = CurrentValueSubject<Float, Never>(0)
public let handleFinish: (URL) -> Void
public let handleError: (Error?) -> Void
public init(identifier: String, handleFinish: #escaping (URL) -> Void, handleError: #escaping (Error?) -> Void) {
self.identifier = identifier
self.handleFinish = handleFinish
self.handleError = handleError
}
lazy var urlSession: URLSession = {
let config = URLSessionConfiguration.background(withIdentifier: self.identifier)
config.sessionSendsLaunchEvents = true
let queue = OperationQueue()
return URLSession(configuration: config, delegate: self, delegateQueue: queue)
}()
func startDownload(url: URL) {
let backgroundTask = urlSession.downloadTask(with: url)
backgroundTask.resume()
}
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
self.handleError(error)
}
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
self.handleFinish(location)
}
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
let percentage = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
self.progress.send(percentage)
}
}
Basically I create a new FileDownloader for each row file. The problems are the following:
On the simulator, I start a download and let it run for a bit, then I stop the app. When I start it again and try to download the file again, it starts downloading 2 of the same file and updates me with the progress of both.
I'm not sure how to get the current progress when I go away from the download page and then come back.
Seems like if I could get the currently running task then I could use that. Is it possible to query the system for a running download and start getting the progress updates? Am I way off on my implementation and need to make changes to do what I want to do?

Related

Swift background download task got suspended when I application goes into background

I am trying to create a download task that will continue to download a video even when the application is in the background.
So I followed the apple documentation to create a url session as background.
When I started the download process by tapped a button, I will print the progress of the download.
However, the application stops printing the progress when the application goes into background.
I wonder what did I miss, or misunderstand.
class ViewController: UIViewController {
private lazy var urlSession: URLSession = {
let config = URLSessionConfiguration.background(withIdentifier: "MySession")
config.isDiscretionary = true
config.sessionSendsLaunchEvents = true
return URLSession(configuration: config, delegate: self, delegateQueue:
OperationQueue())
}()
var downloadUrl = "https://archive.rthk.hk/mp4/tv/2021/THKCCT2021M06000036.mp4"
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func downloadButtonTapped(_ sender: UIButton) {
print("DownloadButton Tapped")
let url = URL(string: downloadUrl)
guard let url = url else {
print("Invalid URL")
return
}
let downloadTask = urlSession.downloadTask(with: url)
downloadTask.resume()
}
}
extension ViewController: URLSessionDownloadDelegate, URLSessionDelegate {
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask,
didFinishDownloadingTo location: URL) {
print("Downloaded, location: \(location.path)")
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask,
didWriteData bytesWritten: Int64, totalBytesWritten: Int64,
totalBytesExpectedToWrite: Int64) {
let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
print("Progress: \(progress)")
}
}
You need to configure the background tasks to make that work properly.
For more details to configure background tasks please check this official apple developer documentation
https://developer.apple.com/documentation/foundation/url_loading_system/downloading_files_in_the_background

How to use URLSession delegate with custom class

I'm used to making a custom class for URLSession using singleton. So I'm using it with URLSession delegate first time and I'm getting confused. Because delegate method is not called!
Can I know any solution? Did I miss something?
And I just want to check whether making a custom class for URLSession is a good thing?
Here is my code. This is my custom API for URLSession.
import UIKit
class CustomNetworkAPI {
static let shared = CustomNetworkAPI()
var session: URLSession?
private var sessionDataTask: URLSessionDataTask?
private var sessionDownloadTask: URLSessionDownloadTask?
var cache: URLCache?
private init() {}
func downloadTaskForImage(_ url: URL, _ completionHandler: #escaping (Result<URL, Error>) -> ()) {
// print(session.delegate)
sessionDownloadTask = session?.downloadTask(with: url, completionHandler: { (url, response, error) in
if let error = error {
completionHandler(.failure(error))
return
}
guard let url = url else {
completionHandler(.failure(URLRequestError.dataError))
return
}
completionHandler(.success(url))
})
sessionDownloadTask?.resume()
}
}
And this is sample code(not whole thing) of VC using the api and delegate.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let api = CustomNetworkAPI.shared
let config = URLSessionConfiguration.default
api.session = URLSession(configuration: config, delegate: self, delegateQueue: nil)
guard let url = URL(string: "...") else { return }
api.downloadTaskForImage(url) { (result) in
switch result {
case .success(let url):
print(url)
case .failure(let error):
print(error)
}
}
}
}
extension ViewController: URLSessionDownloadDelegate {
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
print("down load complete")
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
print("ing")
}
}
I was missing something, which is this quote below that I found in apple docs.
Your URLSession object doesn’t need to have a delegate. If no delegate is assigned, a system-provided delegate is used, and you must provide a completion callback to obtain the data.
So I tried to remove the completion handler block, and the delegate method was called.

Swift: How to catch disk full error on background URLSession.downloadTask?

I'm struggling to understand what I thought would be easy.
I have a URLSession.downloadTask. I have set my downloading object as the URLSession delegate and the following delegate methods do receive calls, so I know my delegate is set correctly.
func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?)
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL)
The case I can't trap is when the downloadTask fills up the disk space on the iPad. None of those delegate methods get called.
How should I catch this error?
Here's my download object:
import Foundation
import Zip
import SwiftyUserDefaults
extension DownloadArchiveTask: URLSessionDownloadDelegate {
// Updates progress info
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask,
didWriteData bytesWritten: Int64, totalBytesWritten: Int64,
totalBytesExpectedToWrite: Int64) {
let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
self.delegate?.updateProgress(param: progress)
}
// Stores downloaded file
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
print("DownloadArchiveTask: In didFinishDownloadingTo")
}
}
extension DownloadArchiveTask: URLSessionTaskDelegate {
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
print("DownloadArchiveTask: In didCompleteWithError")
if error != nil {
print("DownloadArchiveTask: has error")
self.delegate?.hasDiskSpaceIssue()
}
}
}
extension DownloadArchiveTask: URLSessionDelegate {
// Background task handling
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
print("DownloadArchiveTask: In handler for downloading archive")
DispatchQueue.main.async {
let sessionIdentifier = session.configuration.identifier
if let sessionId = sessionIdentifier, let app = UIApplication.shared.delegate as? AppDelegate, let handler = app.completionHandlers.removeValue(forKey: sessionId) {
handler()
}
}
}
func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
print("DownloadArchiveTask: didBecomeInvalidWithError")
if error != nil {
print("DownloadArchiveTask: has error")
self.delegate?.hasDiskSpaceIssue()
}
}
}
class DownloadArchiveTask: NSObject {
var delegate: UIProgressDelegate?
var archiveUrl:String = "http://someurl.com/archive.zip"
var task: URLSessionDownloadTask?
static var shared = DownloadArchiveTask()
// Create downloadsSession here, to set self as delegate
lazy var session: URLSession = {
let configuration = URLSessionConfiguration.background(withIdentifier: "\(Bundle.main.bundleIdentifier!).background_archive")
return URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
}()
func initialDownload() {
// Create URL to the source file you want to download
let fileURL = URL(string: archiveUrl)
let request = URLRequest(url:fileURL!)
self.task = self.session.downloadTask(with: request)
task?.resume()
}
}
Anyone done this before? I can't believe it's this hard - I must be approaching the problem in the wrong way...
I had to solve this problem for my company not that long ago. Now my solution is in Objective C so you'll have to convert it over to Swift, which shouldn't be that hard. I created a method that got the available storage space left on the device and then checked that against the size of the file we're downloading. My solution assumes you know the size of the file you download, in your case you can use the totalBytesExpectedToWrite parameter in the didWriteData method.
Here's what I did:
+ (unsigned long long)availableStorage
{
unsigned long long totalFreeSpace = 0;
NSError* error = nil;
NSArray* paths = NSSearchPathForDirectoriesInDomain(NSDocumentDirectory, NSUserDomainMask, YES);
NSDictionary* dictionary = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject] error:&error];
if (dictionary)
{
NSNumber* freeFileSystemSizeInBytes = [dictionary objectForKey:NSFileSystemFreeSize];
totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
}
return totalFreeSpace;
}
Make sure you leave some room for error with these numbers, since iTunes, the settings app on the device, and this number never match up. The number we get here is the smallest of the three and is in MB. I hope this helps and let me know if you need help converting it to Swift.

How to aggregate multiple child Progress into one master Progress in Swift

I'm trying to download multiple files simultaneously using NSURLSession in Swift. I want to merge all the download progress status into one as to show 100% when all the files are downloaded. Currently I get 100% for each file download completion, but I need 100% only if all the files are downloaded. How can I achieve this in Swift ?
Here is my DownloadManager Class :
class DownloadManager : NSObject, URLSessionDelegate, URLSessionDownloadDelegate {
static var shared = DownloadManager()
var task1Progress = 0.00
var task2Progress = 0.00
typealias ProgressHandler = (Float) -> ()
var onProgress : ProgressHandler? {
didSet {
if onProgress != nil {
let _ = activate()
}
}
}
override private init() {
super.init()
}
func activate() -> URLSession {
let config = URLSessionConfiguration.background(withIdentifier: "\(Bundle.main.bundleIdentifier!).background")
// Warning: If an URLSession still exists from a previous download, it doesn't create a new URLSession object but returns the existing one with the old delegate object attached!
return URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue())
}
func calculateProgress(session : URLSession, completionHandler : #escaping (Float) -> ()) {
session.getTasksWithCompletionHandler { (tasks, uploads, downloads) in
let progress = downloads.map({ (task) -> Float in
if task.countOfBytesExpectedToReceive > 0 {
return Float(task.countOfBytesReceived) / Float(task.countOfBytesExpectedToReceive)
} else {
return 0.0
}
})
completionHandler(progress.reduce(0.0, +))
}
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
if totalBytesExpectedToWrite > 0 {
if let onProgress = onProgress {
calculateProgress(session: session, completionHandler: onProgress)
}
let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
debugPrint("Download Progress \(downloadTask) \(progress)")
}
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
debugPrint("Download finished: \(location)")
try? FileManager.default.removeItem(at: location)
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
debugPrint("Task completed: \(task), error: \(String(describing: error))")
}
private func calculateProgress(session : URLSession, completionHandler : #escaping (Float) -> ()) {
session.getTasksWithCompletionHandler { (tasks, uploads, downloads) in
let progress = downloads.map({ (task) -> Float in
if task.countOfBytesExpectedToReceive > 0 {
if (task.taskIdentifier == 1) {
self.task1Progress = Double(Float(task.countOfBytesReceived) / Float(task.countOfBytesExpectedToReceive))
} else if (task.taskIdentifier == 2){
self.task2Progress = Double(Float(task.countOfBytesReceived) / Float(task.countOfBytesExpectedToReceive))
}
print("pro1 = \(self.task1Progress) pro2 = \(self.task2Progress)")
if(self.task1Progress>0.0000 && self.task2Progress>0.000) {
return Float(min(self.task1Progress ,self.task2Progress))
}
return Float(max(self.task1Progress ,self.task2Progress))
} else {
return 0.0
}
})
completionHandler(progress.reduce(0.0, +))
}
}
}
You can use dispatchgroup tp achieve the desired behaviour.
To download file you will call shared method of downloadManager. Dispatch the downloading operation on dispatchGroup and in notify you will get the callback when all files are downloaded.
Please find example below :
func dispatchGroupUsage (completion: CallBack) {
let backgroundQ = DispatchQueue.global(attributes: .qosDefault)
let group = DispatchGroup()
for number in numberOfFilesToBeDownloaded {
group.enter()
backgroundQ.async(group: group, execute: {
// call download manager's method to download files
group.leave()
})
}
group.notify(queue: DispatchQueue.main, execute: {
print("All Done"); completion(result: fill)
})
}
variables for store data and calculate progress
var progress: Float = 0
var expectedContentLength: Int64 = 0
var allData: Data = Data()
create default session:
let defaultConfiguration = URLSessionConfiguration.default
let defaultSession = URLSession(configuration: defaultConfiguration, delegate: self, delegateQueue: nil
download data from url you can call this method as many times as you want
func downlod(from url: URL, session: URLSession) {
let dataTask = session.dataTask(with: url)
dataTask.resume();
}
and you need to implement following delegates
URLSessionDelegate, URLSessionDataDelegate
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: #escaping (URLSession.ResponseDisposition) -> Swift.Void) {
progress = 0
expectedContentLength += response.expectedContentLength
completionHandler(.allow)
}
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
allData.append(data)
progress = Float(allData.count) / Float(expectedContentLength)
print("progress - \(progress)")
}
don't forget to reset expectedContentLength variable when you starting download, like this
expectedContentLength = 0
I had a similar requirement and this is how I managed to fix, for me I had a list of topics and each topic had chapters and each chapter was a file. So when a topic is downloaded, I had to download its chapters. So here is the catch lets say a topic had 15 files ( I get the number from metadata), there will be 15 download objects and 15 download taskes, whenever each task fires the progress I handle it in the below function in my view.
The idea is simple each file start from 0.0 to 1.0 to finish, so when all 15 finishes the sum of all progress would be 15.0 that means all download gets finished when the sum of progress == total number of files
func handleProgress(percentage:Float){
// Array of files
let totalFileCount = downloadData.count
totalPercentageProgress += percentage
DispatchQueue.main.async {
self.downloadProgressView.progress = self.totalPercentageProgress / Float(totalFileCount)
}
}
-----
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64){
if totalBytesExpectedToWrite > 0 {
let progressPercentage = Float(totalBytesWritten)/Float(totalBytesExpectedToWrite)
let progressHandler = getProgressHandlerForTask(identifier: downloadTask.taskIdentifier)
print(progressPercentage)
delegate?.handleProgress(progressPercentage)
}
}
Just use NSProgress. It allows you to do exactly what you want.
Each NSURLSessionTask has a progress property (if you are targetting an older OS you can create one yourself).
Then just create a parent NSProgress instance and add each task progress as a child.
Finally, observe the fractionCompleted property of NSProgress and update your progress indicator.

Migrating code from NSURLConnection to NSURLSession [duplicate]

In the following code, the file downloads just fine. However none of the delegate methods seem to be called as I receive no output whatsoever. the progressView is not updated either. Any idea why?
import Foundation
import UIKit
class Podcast: PFQueryTableViewController, UINavigationControllerDelegate, MWFeedParserDelegate, UITableViewDataSource, NSURLSessionDelegate, NSURLSessionDownloadDelegate {
func downloadEpisodeWithFeedItem(episodeURL: NSURL) {
var request: NSURLRequest = NSURLRequest(URL: episodeURL)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)
var downloadTask = session.downloadTaskWithURL(episodeURL, completionHandler: { (url, response, error) -> Void in
println("task completed")
if (error != nil) {
println(error.localizedDescription)
} else {
println("no error")
println(response)
}
})
downloadTask.resume()
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
println("didResumeAtOffset")
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
var downloadProgress = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)
println(Float(downloadProgress))
println("sup")
epCell.progressView.progress = Float(downloadProgress)
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
println(location)
}
}
From my testing, you have to choose whether you want to use a delegate or a completion handler - if you specify both, only the completion handler gets called. This code gave me running progress updates and the didFinishDownloadingToURL event:
func downloadEpisodeWithFeedItem(episodeURL: NSURL) {
let request: NSURLRequest = NSURLRequest(URL: episodeURL)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
let downloadTask = session.downloadTaskWithURL(episodeURL)
downloadTask.resume()
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
println("didResumeAtOffset: \(fileOffset)")
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
var downloadProgress = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)
println("downloadProgress: \(downloadProgress)")
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
println("didFinishDownloadingToURL: \(location)")
println(downloadTask)
}
From the NSURLSession documentation, here's the relevant section:
Like most networking APIs, the NSURLSession API is highly asynchronous. It returns data in one of two ways, depending on the methods you call:
To a completion handler block that returns data to your app when a transfer finishes successfully or with an error.
By calling methods on your custom delegate as the data is received.
By calling methods on your custom delegate when download to a file is complete.
So by design it returns data to either a completion handler block or a delegate. But as evinced here, not both.
Interestingly, Apple specifically explains this behavior in their NSURLSessionDataDelegate (but neither in the base delegate NSURLSessionTaskDelegate nor in NSURLSessionDownloadDelegate)
NOTE
An NSURLSession object need not have a delegate. If no delegate is assigned, when you create tasks in that session, you must provide a completion handler block to obtain the data.
Completion handler block are primarily intended as an alternative to using a custom delegate. If you create a task using a method that takes a completion handler block, the delegate methods for response and data delivery are not called.
Swift 3
class ViewController: UIViewController {
var urlLink: URL!
var defaultSession: URLSession!
var downloadTask: URLSessionDownloadTask!
}
// MARK: Button Pressed
#IBAction func btnDownloadPressed(_ sender: UIButton) {
let urlLink1 = URL.init(string: "https://github.com/VivekVithlani/QRCodeReader/archive/master.zip")
startDownloading(url: urlLink!)
}
#IBAction func btnResumePressed(_ sender: UIButton) {
downloadTask.resume()
}
#IBAction func btnStopPressed(_ sender: UIButton) {
downloadTask.cancel()
}
#IBAction func btnPausePressed(_ sender: UIButton) {
downloadTask.suspend()
}
func startDownloading (url:URL) {
let backgroundSessionConfiguration = URLSessionConfiguration.background(withIdentifier: "backgroundSession")
defaultSession = Foundation.URLSession(configuration: backgroundSessionConfiguration, delegate: self, delegateQueue: OperationQueue.main)
downloadProgress.setProgress(0.0, animated: false)
downloadTask = defaultSession.downloadTask(with: urlLink)
downloadTask.resume()
}
// MARK:- URLSessionDownloadDelegate
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
print("File download succesfully")
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
downloadProgress.setProgress(Float(totalBytesWritten)/Float(totalBytesExpectedToWrite), animated: true)
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
downloadTask = nil
downloadProgress.setProgress(0.0, animated: true)
if (error != nil) {
print("didCompleteWithError \(error?.localizedDescription)")
}
else {
print("The task finished successfully")
}
}

Resources