Question
URLSession Delegate Callback Order After Cancellation or Timeout
Neel Nova
0 reputation · 05 Feb 2024, 05:33 UTC
19.7K views0
Uncertain Callback Sequencing
When a URLSession data task is cancelled via task.cancel() or by cancelling the surrounding Task in Swift 5.5’s async/await API, the underlying CFNetwork stack may still invoke delegate methods with partial data. The documentation does not specify whether urlSession(_:task:didCompleteWithError:) is guaranteed to fire after all urlSession(_:task:didReceive:) callbacks, or whether the order can vary.
Specific Unresolved Questions
- Does
urlSession(_:task:didCompleteWithError:)always occur after the lastdidReceivecallback when a task is cancelled? - Can the completion callback be invoked before any final data callbacks in a cancellation scenario?
- In async/await, if a task is cancelled while data is being streamed, does the
data(for:)call return any data before throwingURLError.cancelled?