Hi developers,
I'm facing a weird issue with URLSessionUploadTask.
I created a URLSession using background config:
let config = URLSessionConfiguration.background(withIdentifier: identifier)
config.httpMaximumConnectionsPerHost = 1
config.allowsCellularAccess = true
config.sessionSendsLaunchEvents = true
config.waitsForConnectivity = true
let backgroundURLSession = URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue.main)
Started a task using this code:
let task = backgroundURLSession?.uploadTask(with: urlRequest, fromFile: fileURL)
task?.taskDescription = "someIdentifier"
task?.resume()
Now when I'm trying to cancel this task (whenever the user presses the cancel button on UI), the cancel() method is not calling the delegate method as mentioned in the documentation.
urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
Code used to cancel the task:
backgroundURLSession?.getAllTasks { tasks in
for task in tasks where task.taskDescription == identifier {
task.cancel()
break
}
}
While debugging I can see that task.cancel() is getting called and the state of the task is getting changed from 0 i.e. running to 2 i.e.canceling.
Any help would be appreciated. Thanks in advance!