URLSessionTaskDelegate didCompleteWithError

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:

Answered by DTS Engineer in 753030022

When you create a task in a session, you can use either a standard method or a convenience method. For example:

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"

Accepted Answer

When you create a task in a session, you can use either a standard method or a convenience method. For example:

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"

URLSessionTaskDelegate didCompleteWithError
 
 
Q