-
Notifications
You must be signed in to change notification settings - Fork 111
Add ability to cancel uploads and downloads #713
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
7e42592 to
87532fb
Compare
Co-Authored-By: James Lawton <jlawton@box.com>
Co-Authored-By: James Lawton <jlawton@box.com>
Co-Authored-By: James Lawton <jlawton@box.com>
Co-Authored-By: James Lawton <jlawton@box.com>
Co-Authored-By: James Lawton <jlawton@box.com>
Co-Authored-By: James Lawton <jlawton@box.com>
Co-Authored-By: James Lawton <jlawton@box.com>
jlawton
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it works to me, and seems like a reasonable evolution of the code.
I think that Combine (which I know we can't use) has a slightly cleaner solution to this sort of thing, but it hinges on the fact that Publishers separate initialization and subscription, and that they are chained, rather than using callbacks. I think moving in that direction would be too large of a change.
This wouldn't be totally trivial to implement. Just to document the idea in case you're interested in an alternative, it would look something like:
class NetworkTask<T> {
/// Run a task
func subscribe(_ completion: @escaping Callback<T>) -> Cancellable
/// Chain tasks
func then<U>(_ nextTask: (T) -> NetworkTask<U>) -> NetworkTask<U>
}
func preflightCheck(…) -> NetworkTask<Void>
func upload(…) -> NetworkTask<File>
func uploadWithPreflightCheck(…) -> NetworkTask<File> {
return preflightCheck(…).then { upload(…) }
}
func uploadWithPreflightCheck(…, _ completion: @escaping Callback<File>) -> Cancellable {
return uploadWithPreflightCheck(…).subscribe(completion)
}
Goals ⚽
Implementation Details 🚧
BoxDownloadTaskreturned for every download request. This task contains the URLSessionTask that is associated with that request.BoxDownloadTaskhas a cancel method on it that can be called. When called, it cancels the URLSessionTask associated with, resulting in a cancelled request.BoxUploadTaskreturned for every upload request.BoxUploadTaskhas a cancel method on it that can be called.Testing Details 🔍