In an effort to get more visibility to when there are network related errors, I tried implementing URLSessionTaskDelegate's didCompleteWithError function. However, this delegate function is never called although there are tasks that fail. URLSessionDelegate functions work as expected. Also, URLSessionTaskDelegate's didCreateTask function is called as expected.
Is there more nuance that I'm missing concerning when didCompleteWithError should be called?
Some additional context:
- Our URLSession delegate conforms to
URLSessionDelegate,URLSessionTaskDelegate, andURLSessionDataDelegate- All 3 protocols work as expected except didCompleteWithError
- The app in question integrates with BlackBerry Dynamics SDK. However, their documentation does not say that
didCompleteWithErroris effected by the SDK.- https://developer.blackberry.com/files/blackberry-dynamics/ios/nsurlsession_support.html
- The implementation of creating a task can be seen here: https://github.com/MFB-Technologies-Inc/NetworkService/blob/main/Sources/NetworkService/NetworkServiceClient%2BStart.swift
When you create a task in a session, you can use either a standard method or a convenience method. For example:
-
The
dataTask(with:)method is the standard way to create a data task. -
The
dataTask(with:completionHandler:)method is the convenience way to create a data task. Note that it takes a completion handler that’s called when the task completes.
If you call a convenience method in a session that has a delegate, the system only calls the delegate methods that aren’t covered by the completion handler. So, for the above example, it won’t called urlSession(_:task:didCompleteWithError:) because the error status is being delivered by the completion handler.
Based on the links you posted it seems like you’re calling a Swift async method. That is basically a wrapper around the convenience method and so it won’t call your delegate’s urlSession(_:task:didCompleteWithError:).
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"