Skip to content

Add URLSessionDownloadTask.cancel(byProducingResumeData:) #1592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

joshavant
Copy link
Contributor

@joshavant joshavant commented Jun 9, 2018

This is an attempt at resolving the implementation for URLSessionDownloadTask.cancel(byProducingResumeData:).

This function seems to represent an edge case in portability. After communicating with @phausler, it appears the Objective-C implementation of this relies on an Apple-maintained XPC process to manage the bookmarking of partially downloaded data. Of course, this behavior is not portable to the framework project.

One approach that was considered was serializing the URLSessionDownloadTask instance and returning that to the caller as the opaque blob given to the completion handler. However, the Objective-C implementation of URLSessionTask does not implement any NSCoding flavor publicly, so conformance would need to be implemented in this framework with internal or greater access control. But, unfortunately, that functionality is not currently supported in Swift.

The approach suggested by @phausler was simply to... hardcode calling the completion block. This is what was done in this PR.

The only thing worth highlighting about the approach is that a token Data instance is passed to the completion block to prevent unexpected failures that may be caused from passing nil instead.

Open Issues:

  • downloadTask(withResumeData:) (and related) would remain broken by this approach. What should be done with these functions?
  • Unit test(s)

@pushkarnk
Copy link
Member

@swift-ci please test

*
* Instead, we just call the completionHandler directly.
*
* A token Data value is passed to the client to prevent unexpected failures
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be prudent if data was usually expected, but the contract of this method goes:

If the download is resumable, the completion handler is provided with a resumeData object. Your app can later pass this object to a session’s downloadTask(withResumeData:) or downloadTask(withResumeData:completionHandler:) method to create a new task that resumes the download where it left off.

Since we do not yet support resuming on Linux, no downloads are resumable, and so we can safely pass nil here. Let's not confuse downstream clients into thinking the download is resumable when we can't resume it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point. PR updated, accordingly.

Copy link
Contributor

@millenomi millenomi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'll request a change. This breaks the contract.

@pushkarnk
Copy link
Member

@swift-ci please test

@millenomi
Copy link
Contributor

@swift-ci please test

@joshavant
Copy link
Contributor Author

🎉

What's the process to merge this in, now that everything is passing?

@millenomi
Copy link
Contributor

Me hitting a button.

@millenomi millenomi merged commit b3cf307 into swiftlang:master Jun 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants