Skip to content
This repository has been archived by the owner on Aug 29, 2022. It is now read-only.

How to use URLSession's Progress #230

Closed
piercifani opened this issue May 9, 2018 · 4 comments
Closed

How to use URLSession's Progress #230

piercifani opened this issue May 9, 2018 · 4 comments
Labels
bug The framework isn't behaving as expected.
Milestone

Comments

@piercifani
Copy link
Contributor

Maybe this is more of a Progress question instead of Deferred's, but here it is anyways:

Is there a way to pass the progress of a URLSessionTask as the progress to create a Task?

Executing the following code launches an exception:

extension URLSession {
    public func fetchData(with urlRequest: URLRequest) -> Task<Data> {
        let deferred = Deferred<Task<Data>.Result>()
        let task = self.dataTask(with: urlRequest) { (data, response, error) in
             ...
        }
        task.resume()
        if #available(iOS 11.0, *) {
            return Task(deferred, progress: task.progress)
        } else {
            return Task(deferred, cancellation: { [weak task] in
                task?.cancel()
            })
        }
    }

2018-05-09 13:08:35.030826+0200 xctest[74737:3872356] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSProgress addChild:withPendingUnitCount:]: NSProgress 0x7fbc49824650 was already the child of another progress 0x7fbc49915bc0'

@zwaldowski
Copy link
Contributor

Hm, that’s exactly how we’re doing it in several projects. I’ll look into it, thanks as always! Can you provide any additional information about how you’re calling the code you pasted, like through andThen?

@piercifani
Copy link
Contributor Author

Sure! here's how the Tasks are chained.

This PR doesn't use the URLSession's progress for the Task creation because it was crashing with that exception. If you want to reproduce it locally, you could clone the repo and hook the task.progress here and then Command+U

@zwaldowski zwaldowski added this to the 3.3.2 milestone May 12, 2018
@zwaldowski
Copy link
Contributor

Adding a test case for this, seems legitimate.

@piercifani
Copy link
Contributor Author

Thanks a lot @zwaldowski !!!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug The framework isn't behaving as expected.
Projects
None yet
Development

No branches or pull requests

2 participants