1

It is possible to receive progress updates about a URLSessionTask by implementing the URLSessionDataDelegate.urlSession(_:dataTask:didReceive:) delegate method, and using the delegate-style "task, task.resume()" style of invocation.

With the Combine flavor of the API, this does not appear to be possible. Using URLSession.dataTaskPublisher(for:) returns a publisher that publishes the (Data, URLResponse) tuple upon completion, but never invokes the delegate method. In that way, it is very similar to the URLSession.dataTask(with:completionHandler:) method, which invokes completionHandler with the final result, and not in-process Progress reports.

Am I missing any API or pattern to allow progress reporting, or does the Combine flavor of URLSession task handling not offer a way to retrieve progress?

Palpatim
  • 9,074
  • 35
  • 43

1 Answers1

4

No, you're not missing anything. To retrieve the progress information you would need to construct your own Future and not use the built-in data task publisher.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks for confirming. Have you seen any useful patterns for publishing more than one stream from an API? I'm currently looking at returning an object with various `*publisher` properties (e.g., `resultPublisher`, `progressPublisher`), which feels icky. The other thing I'm considering is accepting a `PassthroughSubject` on the API for interested folks to be notified, but again...icky. – Palpatim Jun 29 '20 at 21:25
  • Actually I don't see why result publisher and progress publisher are icky. – matt Jun 29 '20 at 21:40
  • Also there's already some good discussion https://stackoverflow.com/questions/61102801/how-do-i-check-the-current-progress-of-urlsession-datataskpublisher and https://stackoverflow.com/questions/57836518/implementing-reconnection-with-urlsession-publisher-and-combine – matt Jun 29 '20 at 21:43