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

Adjust naming of offline tile downloading methods #353

Merged
merged 7 commits into from
Jul 15, 2019

Conversation

akitchen
Copy link
Contributor

Fixes #352

@frederoni
Copy link
Contributor

The flaky GeoJSON tests may be caused by unsorted JSON.

@akitchen
Copy link
Contributor Author

akitchen commented Feb 21, 2019

The flaky GeoJSON tests may be caused by unsorted JSON.

I ran it again and it passed. Can't seem to reproduce locally.

where is the non-determinisim? there seems to be a timeout in the test, which could indicate the OHHTTPStubs isn't yet bootstrapped properly, or there's a race. I think we've seen this before with NSURLProtocol-based network stubbing in tests.

I don;t think this should block merge of this PR (assuming we're 👍 otherwise) but we do need to get to the bottom of it.

@frederoni
Copy link
Contributor

where is the non-determinisim?

I can't find the non-deterministic URL here, but it MapboxStatic.swift, the OHHTTPStub parsed GeoJSON when setting up the router which would lead to undeterministic route/url.

We worked around that in the test case by adding:
https://github.com/mapbox/MapboxStatic.swift/blob/3ab49ce2f8f51baee2895f5bef5b26721c41b5a4/MapboxStatic/Overlay.swift#L399-L425

Copy link
Contributor

@1ec5 1ec5 left a comment

Choose a reason for hiding this comment

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

Thanks for taking this one. Apart from the feedback below, the changelog entry should mention both the addition of data task methods and the fact that these methods now resume their tasks, so the postcondition is no longer required.

@discardableResult
func fetchAvailableOfflineVersions(completionHandler: @escaping OfflineVersionsHandler) -> URLSessionDataTask
@objc(fetchAvailableOfflineVersionsWithCompletion:)
func fetchAvailableOfflineVersions(completionHandler: @escaping OfflineVersionsHandler) -> ()
Copy link
Contributor

@1ec5 1ec5 Feb 21, 2019

Choose a reason for hiding this comment

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

I think it’s still useful to return a data task, as the “calculate” methods in Directions do. That way the application can cancel the task if necessary. Sort of the “scheduled timer” pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh, I missed this comment earlier. does that render this PR (and #352) moot @1ec5 ?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, I think the important thing is that this method resumes the task. It’s fine that it also returns the task.

MapboxDirections/OfflineDirections.swift Outdated Show resolved Hide resolved
MapboxDirections/OfflineDirections.swift Outdated Show resolved Hide resolved
@akitchen
Copy link
Contributor Author

I'm not so sure the JSON keys sort order is a factor here - the stub is matching on:

        let queryParams: [String: String?] = [
            "alternatives": "true",
            "geometries": String(describing: shapeFormat),
            "overview": "full",
            "steps": "true",
            "continue_straight": "true",
            "access_token": BogusToken,
        ]
        stub(condition: isHost("api.mapbox.com")
            && isPath("/directions/v5/mapbox/driving/-122.42,37.78;-77.03,38.91.json")
            && containsQueryParams(queryParams)) { _ in
[...]

... so I interpret the expectation timeout to mean that it isn't set up, or that internal async within the URL loading system tearing down the previous stubs caught this one in its dragnet as well.

Since these stubs are so specific and localized, I think we can let them live longer and tear them down at the end of this test class's execution. Small commit coming to test this theory.

@akitchen
Copy link
Contributor Author

Interestingly, the test still failed with eeb41aa (which passes locally). 🤔

@akitchen akitchen force-pushed the akitchen/adjust-offline-tile-methods branch from cd1f4d1 to fff15d4 Compare February 21, 2019 23:36
@akitchen
Copy link
Contributor Author

I reverted the change to test teardown since it seems to incur the same level of flakiness. We can move the discussion of JSON key sorting into #354 and try to keep this focused and get it merged, or not merged, depending on #353 (comment) (cc @1ec5 )

@discardableResult
func downloadTiles(in coordinateBounds: CoordinateBounds, version: OfflineVersion, session: URLSession?, completionHandler: @escaping OfflineDownloaderCompletionHandler) -> URLSessionDownloadTask
@objc(downloadTilesInCoordinateBounds:withVersion:completionHandler:)
func downloadTiles(in coordinateBounds: CoordinateBounds, version: OfflineVersion, completionHandler: @escaping OfflineDownloaderCompletionHandler) -> ()
Copy link
Contributor

Choose a reason for hiding this comment

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

Either the argument label in Swift should be with: or the selector piece in Objective-C should be just version:.

Copy link

Choose a reason for hiding this comment

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

@1ec5 should we plan a major release for this?

@frederoni frederoni force-pushed the akitchen/adjust-offline-tile-methods branch from ec2f3f6 to 3b873cc Compare June 24, 2019 18:36
*/
@discardableResult
@objc(availableVersionsDataTaskWithCompletionHandler:)
public func availableVersionsDataTask(completionHandler: @escaping OfflineVersionsHandler) -> URLSessionDataTask {
let task = URLSession.shared.dataTask(with: availableVersionsURL) { (data, response, error) in
if let error = error {
return completionHandler(nil, error)
Copy link
Contributor

Choose a reason for hiding this comment

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

availableVersionsDataTask(_:) still confuses me; I don’t really see the point in keeping it separate. In general, for any kind of API request, the developer would only ever need two kinds of methods:

  • A method that encapsulates requesting and processing a response, taking a completion handler and returning a (discardable) task for cancellation.
  • A method or property that provides access to the request payload itself without side effects.

These requirements are satisfied by fetchAvailableOfflineVersions(_:) and availableVersionsURL, and by downloadTiles(in:version:) and tilesURL(for:version:). availableVersionsDataTask(_:) and routingTilesDataTaskWith(_:version:completionHandler:) are confusing hybrids, because they look like accessors or factory methods but take completion handlers, as if they’re the methods that perform the task. In fact, completionHandler is documented as being “called when the request completes”, but calling the method by itself doesn’t cause the completion handler to ever get called.

We can leave these methods in, but we should probably remove @discardableResult because the methods do nothing on their own.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree. The hybrid methods don't improve readability or versatility so I folded them into their counterparts.

@frederoni
Copy link
Contributor

ready for review
cc @1ec5

@1ec5 1ec5 merged commit ade12fb into master Jul 15, 2019
@1ec5 1ec5 deleted the akitchen/adjust-offline-tile-methods branch July 15, 2019 17:37
@1ec5 1ec5 added the backwards incompatible changes that break backwards compatibility of public API label Jul 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backwards incompatible changes that break backwards compatibility of public API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Offline download task methods masquerade as action methods
4 participants