Skip to content
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

Directions methods should return some kind of Cancellable protocol instead of URLSessionDataTask #465

Open
SebastianOsinski opened this issue Sep 23, 2020 · 1 comment
Labels
op-ex Refactoring, Tech Debt or any other operational excellence work.

Comments

@SebastianOsinski
Copy link
Contributor

Right now all methods in Directions class return URLSessionDataTask instance directly. This makes subclassing Directions harder, as it leaks internal implementation of it (i.e. it uses URLSession underneath). As far as I noticed, returned tasks are only used for cancelling them, so it is possible to just introduce Cancellable protocol in form of:

public protocol Cancellable {
    func cancel()
}

with extension for URLSessionDataTask:

extension URLSessionDataTask: Cancellable {}

and declare all methods to return it instead of URLSessionDataTask. This will allow to make subclasses of Directions (e.g. one using other web service than Mapbox's one or some offline solution) without being tied to URLSession.

@1ec5
Copy link
Contributor

1ec5 commented Sep 25, 2020

This will allow to make subclasses of Directions (e.g. one using other web service than Mapbox's one or some offline solution) without being tied to URLSession.

As a workaround for now, you can call that subclass’s url(forCalculating:) or urlRequest(forCalculating:), pass the return value into your third-party networking library, and transform the resulting data into a RouteResponse using JSONDecoder. Historically, url(forCalculating:) was this library’s answer for using a third-party library, but a protocol would make the library more flexible.

@1ec5 1ec5 added the op-ex Refactoring, Tech Debt or any other operational excellence work. label Sep 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
op-ex Refactoring, Tech Debt or any other operational excellence work.
Projects
None yet
Development

No branches or pull requests

2 participants