NSURLSession in background didCompleteWithError - ios

I'm fighting with NSURLSession upload & download, everything seems to work if not for the fact that, for example, at in the background of 100 files sent (upload) the :
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
not always invoked by the system, regardless of the file sent correctly.
More is almost always invoked at the end of the session ...
It is normal behavior or am I making mistakes ... ??
I also did some testing using AFNetworking but I always get the same behavior.
thanks !

In my case delegate method func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) was not called in Swift 3.0 because it still depends on Objective-C. So please use next code for delegate method:
#objc(URLSession:task:didCompleteWithError:)
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
//your code here
}

Related

NSURLSession .waitsForConnectivity flag ignored when DataTask being run on OperationQueue

I have a handwritten class MyURLRequest, that implements Operation. Inside it creates URLSession, configures it
public init(shouldWaitForConnectivity: Bool, timeoutForResource: Double?) {
baseUrl = URL(string: Self.relevantServerUrl + "api/")
self.shouldWaitForConnectivity = shouldWaitForConnectivity
self.timeoutForResource = timeoutForResource
super.init()
localURLSession = URLSession(configuration: localConfig, delegate: self, delegateQueue: nil)
}
public var localConfig: URLSessionConfiguration {
let res = URLSessionConfiguration.default
res.allowsCellularAccess = true
if let shouldWaitForConnectivity = shouldWaitForConnectivity {
res.waitsForConnectivity = shouldWaitForConnectivity
if let timeoutForResource = timeoutForResource {
res.timeoutIntervalForResource = timeoutForResource
}
}
return res
}
creates URLRequest, dataTask, and then being run on OperationQueue. Operation's methods looks like this
override open func start() {
if isCancelled {
isFinished = true
return
}
startDate = Date()
sessionTask?.resume()
localURLSession.finishTasksAndInvalidate()
}
override open func cancel() {
super.cancel()
sessionTask?.cancel()
}
MyURLRequest also implements URLSessionDataDelegate and URLSessionTaskDelegate and the being delegate for it's own URLSession.
There is a problem with waitsForConnectivity NSURLSessionConfiguration's flag. In constructor I set it to true, but this flag is being ignored. In runtime, when network is turned off, request finishes immediately with error -1009. URLSessionTaskDelegate's method urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) is triggered immediately. func urlSession(_ session: URLSession, taskIsWaitingForConnectivity task: URLSessionTask) not being called at all.
The reason definitely not is that flag waitsForConnectivity wasn't correctly set: I've checked config in task received by urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?), and waitsForConnectivity == true.
I also tried to make request without operation queue, and that went fine - behaved such as expected. Maybe have something to do with OperationQueue. Would appreciate your help!
UPDATE:
Seems like root of the problem is that Operation being released too early (when request not complete yet). I've tried to synchronise them using DispatchGroup():
override open func start() {
if isCancelled {
isFinished = true
return
}
startDate = Date()
dispatchGroup.enter()
sessionTask?.resume()
dispatchGroup.wait()
localURLSession.finishTasksAndInvalidate()
}
where .leave() is called in URLSessionDelegate's methods. Nothing changed, still not waiting for connectivity.
UPDATE:
Here's the error I get in didCompleteWithError:
Error Domain=NSURLErrorDomain Code=-1009 "" UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x7fc319112de0 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <6388AD46-8497-40DF-8768-44FEBB84A8EC>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <6388AD46-8497-40DF-8768-44FEBB84A8EC>.<1>",
"LocalDataTask <26BCBD73-FC8B-4A48-8EA2-1172ABB8093C>.<1>"
), NSLocalizedDescription=., NSErrorFailingURLStringKey=}
I believe the issue is rooted in your use of finishTasksAndInvalidate. It looks like you are depending on that method synchronously waiting for all pending tasks to complete, but according to the documentation that isn't how it works.
Here's a more in-depth explanation of what I think is happening. By default an Operation is considered complete as soon as the start method returns. This is definitely not what you need, as the task has to be completed asynchronously. Operation is not capable of supporting this behavior out of the box.
Your start returns immediately, long before the session has any time to complete the task you have started. Then, with the operation complete, the queue removes it. This often ends up being the only owner of that instance. If that's true, it kicks off the operation deinit process, which ends up releasing the URLSession. At that point, the session looks like it might do some clean up and terminate any outstanding tasks, forwarding some calls to its delegate. I wasn't sure if URLSession does this, but based on what you are seeing, it sounds like it may.
To achieve what you want, I think you'll need to restructure your NSOperation subclass to be fully asynchronous and to only complete when the started task is done.
Building out a fully thread-safe async NSOperation subclass is a real pain. In case this isn't something you've tackled before, you can check out an implementation here: https://github.com/ChimeHQ/OperationPlus

Alamofire Version 5.2.0 and 5.2.1 both Crash at URL Session delegate

I am using latest alamofire version 5.2.1 to make WEB API calls it working perfect with all apis
But when i call api for country list it crash at URL Session Delegate class
open func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
eventMonitor?.urlSession(session, dataTask: dataTask, didReceive: data)
if let request = request(for: dataTask, as: DataRequest.self) {
request.didReceive(data: data)
} else if let request = request(for: dataTask, as: DataStreamRequest.self) { //Crash at this line
request.didReceive(data: data)
} else {
assertionFailure("dataTask did not find DataRequest or DataStreamRequest in didReceive")
return
}
}
The same request works perfect with normal url session and postman only alamofire cannot handle the response i am attaching screen shot for more detail.
Response contains 2000 line json but i don't think that's the problem
Hello this issue already reported on Alamofire
Contributor said this issue was fixed by moving to Xcode 11.4.1
Please check this
https://github.com/Alamofire/Alamofire/issues/3163

How to use a variable from urlSession from another class

I am trying to download multiple files sequentially using urlSession.
I have this code that is in an extension in a different class. I would like to be able to use the variable progress in the viewcontroller class in order to use it to show the download progression on the screen.
How can I pass this variable from an extension from a different class into the viewcontroller class??
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
let progress = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite) * 100
Throw away your progress variable. The URLSessionDownloadTask itself vends a progress object (of the Progress class). It is made to solve exactly this sort of problem, because a Progress object is observable from anywhere (or it can be assigned to a UIProgressView's observedProgress directly). Just research the Progress class and it will be clear what to do.

simple URLSession uploadTask with progress bar

i struggle totally with the URLSession and uploadTask. Actually I just want to upload a json to a Webserver and while the upload is in progress a simple progressbar should be shown.
I implemented the approach given by apple: https://developer.apple.com/documentation/foundation/url_loading_system/uploading_data_to_a_website
the upload is working and i get a response.. so far everything is fine but i don't know how i can show a during the upload a progress bar. What I tried is to show a progressbar before i call the method that contains the upload task
startActivityIndicator()
let jsonPackage = JSONIncident(incident: incident)
jsonPackage.sendToBackend(completion: {
message, error in
//Handle the server response
})
self.activityIndicator.stopAnimating()
UIApplication.shared.endIgnoringInteractionEvents()
I realized that's stupid because the upload task works asynchronious in another thread.
I guess that i have to use Delegates but i don't know in which way. If i implement URLSessionTaskDelegate for example, i have to implement a bunch of functions like isProxy(), isKind(), isMember etc.
Could you please provide me a simple example how to show a progress bar during the uploadtask is working?
That would be so greate!
Thank you very much
regardsChris
You need to conform to the URLSessionTaskDelegate protocol and call this delegate method:
func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64)
{
let uploadProgress = Float(totalBytesSent) / Float(totalBytesExpectedToSend)
}
Then use the uploadProgress variable for your progress bar.
Create your session like this:
var session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: OperationQueue.main)
You can use alamofire(for api calling) with swiftyJSON(json parsing) library for uploading JSON. You just have to call a loader before the alamofire api get called and stop the loader when you will get the response.

WatchOS 3 WKApplicationRefreshBackgroundTask didReceiveChallenge

I have finally (ignoring the sample code which I never saw work past "application task received, start URL session") managed to get my WatchOS3 code to start a background URL Session task as follows:
func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
for task in backgroundTasks {
if let refreshTask = task as? WKApplicationRefreshBackgroundTask {
// this task is completed below, our app will then suspend while the download session runs
print("application task received, start URL session")
let request = self.getRequestForRefresh()
let backgroundConfig = URLSessionConfiguration.background(withIdentifier: NSUUID().uuidString)
backgroundConfig.sessionSendsLaunchEvents = true
backgroundConfig.httpAdditionalHeaders = ["Accept":"application/json"]
let urlSession = URLSession(configuration: backgroundConfig, delegate: self, delegateQueue: nil)
let downloadTask = urlSession.downloadTask(with: request)
print("Dispatching data task at \(self.getTimestamp())")
downloadTask.resume()
self.scheduleNextBackgroundRefresh(refreshDate: self.getNextPreferredRefreshDate())
refreshTask.setTaskCompleted()
}
else if let urlTask = task as? WKURLSessionRefreshBackgroundTask {
//awakened because background url task has completed
let backgroundConfigObject = URLSessionConfiguration.background(withIdentifier: urlTask.sessionIdentifier)
self.backgroundUrlSession = URLSession(configuration: backgroundConfigObject, delegate: self, delegateQueue: nil) //set to nil in task:didCompleteWithError: delegate method
print("Rejoining session ", self.backgroundUrlSession as Any)
self.pendingBackgroundURLTask = urlTask //Saved for .setTaskComplete() in downloadTask:didFinishDownloadingTo location: (or if error non nil in task:didCompleteWithError:)
} else {
//else different task, not handling but must Complete all tasks (snapshot tasks hit this logic)
task.setTaskCompleted()
}
}
}
However, the issue I am now seeing is that my delegate method
urlSession:task:didReceiveChallenge: is never being hit, so I cannot get my download to complete. (I have also added the session level urlSession:didReceiveChallenge: delegate method and it is also not being hit).
Instead I immediately hit my task:didCompleteWithError: delegate method which has the error:
"The certificate for this server is invalid. You might be connecting to a server that is pretending to be ... which could put your confidential information at risk."
Has anyone gotten the background watch update to work with the additional requirement of hitting the didReceiveChallenge method during the background URL session?
Any help or advice you can offer is appreciated.
As it turns out the server certificate error was actually due to a rare scenario in our test environments. After the back end folks gave us a work around for that issue this code worked fine in both our production and test environments.
I never hit urlSession:task:didReceiveChallenge: but it turned out I did not need to.
Made a minor un-related change:
Without prints/breakpoints I was sometimes hitting task:didCompleteWithError Error: like a ms before I hit downloadTask:didFinishDownloadingTo location:.
So I instead set self.pendingBackgroundURLTask completed in downloadTask:didFinishDownloadingTo location:. I only set it completed in task:didCompleteWithError Error: if error != nil.
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
//Complete task only if error, if no error it will be completed when download completes (avoiding race condition)
if error != nil {
self.completePendingBackgroundTask()
}
}
func completePendingBackgroundTask()
{
//Release the session
self.backgroundUrlSession = nil
//Complete the task
self.pendingBackgroundURLTask?.setTaskCompleted()
self.pendingBackgroundURLTask = nil
}
Hope someone else finds this helpful.

Resources