Skip to content

Commit 2214bab

Browse files
committed
Merge pull request #36 from mapbox/1ec5-concurrency
Support concurrent requests
2 parents 849c8c1 + c0c5426 commit 2214bab

File tree

3 files changed

+22
-42
lines changed

3 files changed

+22
-42
lines changed

MapboxGeocoder/MBGeocoder.swift

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,52 +35,27 @@ public class MBGeocoder: NSObject {
3535
self.init(accessToken: accessToken, host: nil)
3636
}
3737

38-
private var task: NSURLSessionDataTask?
38+
private static let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
39+
public var session = NSURLSession(configuration: sessionConfiguration)
3940

40-
private var errorForSimultaneousRequests: NSError {
41-
let userInfo = [
42-
NSLocalizedFailureReasonErrorKey: "Cannot geocode on an MBGeocoder object that is already geocoding.",
43-
]
44-
return NSError(domain: MBGeocoderErrorDomain, code: -1, userInfo: userInfo)
45-
}
46-
47-
public var geocoding: Bool {
48-
return task?.state == .Running
49-
}
50-
51-
public func reverseGeocodeLocation(location: CLLocation, completionHandler: MBGeocodeCompletionHandler) {
52-
guard !geocoding else {
53-
completionHandler(nil, errorForSimultaneousRequests)
54-
return
55-
}
56-
41+
public func reverseGeocodeLocation(location: CLLocation, completionHandler: MBGeocodeCompletionHandler) -> NSURLSessionDataTask? {
5742
let query = String(format: "%.5f,%.5f", location.coordinate.longitude, location.coordinate.latitude)
5843
let router = MBGeocoderRouter.V5(configuration, false, query, nil, nil, nil, nil)
59-
task = taskWithRouter(router, completionHandler: completionHandler)
44+
return taskWithRouter(router, completionHandler: completionHandler)
6045
}
6146

6247
// public func geocodeAddressDictionary(addressDictionary: [NSObject : AnyObject],
6348
// completionHandler: MBGeocodeCompletionHandler)
6449

65-
public func geocodeAddressString(addressString: String, withAllowedScopes scopes: [MBPlacemark.Scope]? = nil, nearLocation focusLocation: CLLocation? = nil, inCountries ISOCountryCodes: [String]? = nil, completionHandler: MBGeocodeCompletionHandler) {
66-
guard !geocoding else {
67-
completionHandler(nil, errorForSimultaneousRequests)
68-
return
69-
}
70-
50+
public func geocodeAddressString(addressString: String, withAllowedScopes scopes: [MBPlacemark.Scope]? = nil, nearLocation focusLocation: CLLocation? = nil, inCountries ISOCountryCodes: [String]? = nil, completionHandler: MBGeocodeCompletionHandler) -> NSURLSessionDataTask? {
7151
let router = MBGeocoderRouter.V5(configuration, false, addressString, ISOCountryCodes, focusLocation?.coordinate, scopes, nil)
72-
task = taskWithRouter(router, completionHandler: completionHandler)
52+
return taskWithRouter(router, completionHandler: completionHandler)
7353
}
7454

7555
// public func geocodeAddressString(addressString: String, inRegion region: CLRegion, completionHandler: MBGeocodeCompletionHandler)
7656

7757
private func taskWithRouter(router: MBGeocoderRouter, completionHandler completion: MBGeocodeCompletionHandler) -> NSURLSessionDataTask? {
78-
return router.loadJSON(JSON.self) { [weak self] (json, error) in
79-
guard let dataTaskSelf = self where dataTaskSelf.task?.state == .Completed
80-
else {
81-
return
82-
}
83-
58+
return router.loadJSON(session, expectedResultType: JSON.self) { (json, error) in
8459
guard error == nil && json != nil else {
8560
dispatch_sync(dispatch_get_main_queue()) {
8661
completion(nil, error as? NSError)
@@ -94,10 +69,11 @@ public class MBGeocoder: NSObject {
9469
dispatch_sync(dispatch_get_main_queue()) {
9570
completion(placemarks, error as? NSError)
9671
}
97-
}
72+
} as? NSURLSessionDataTask
9873
}
9974

10075
public func cancelGeocode() {
101-
task?.cancel()
76+
session.invalidateAndCancel()
77+
session = NSURLSession(configuration: MBGeocoder.sessionConfiguration)
10278
}
10379
}

MapboxGeocoderTests/ForwardGeocodingTests.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,17 @@ class ForwardGeocodingTests: XCTestCase {
2323

2424
let geocoder = MBGeocoder(accessToken: BogusToken)
2525
var addressPlacemark: MBPlacemark! = nil
26-
geocoder.geocodeAddressString("1600 pennsylvania ave nw", inCountries: ["CA"]) { (placemarks, error) in
26+
let task = geocoder.geocodeAddressString("1600 pennsylvania ave nw", inCountries: ["CA"]) { (placemarks, error) in
2727
XCTAssertEqual(placemarks?.count, 5, "forward geocode should have 5 results")
2828
addressPlacemark = placemarks![0]
2929

3030
expectation.fulfill()
3131
}
32+
XCTAssertNotNil(task)
3233

3334
waitForExpectationsWithTimeout(1) { (error) in
3435
XCTAssertNil(error, "Error: \(error)")
35-
XCTAssertFalse(geocoder.geocoding)
36+
XCTAssertEqual(task?.state, .Completed)
3637
}
3738

3839
XCTAssertEqual(addressPlacemark.description, "Pennsylvania Ave, Stellarton, Nova Scotia B0K 1S0, Canada", "forward geocode should populate description")
@@ -63,14 +64,15 @@ class ForwardGeocodingTests: XCTestCase {
6364

6465
let expection = expectationWithDescription("forward geocode execute completion handler for invalid query")
6566
let geocoder = MBGeocoder(accessToken: BogusToken)
66-
geocoder.geocodeAddressString("Sandy Island, New Caledonia", withAllowedScopes: [.AdministrativeArea, .Place, .Locality, .PointOfInterest], inCountries: ["FR"]) { (placemarks, error) in
67+
let task = geocoder.geocodeAddressString("Sandy Island, New Caledonia", withAllowedScopes: [.AdministrativeArea, .Place, .Locality, .PointOfInterest], inCountries: ["FR"]) { (placemarks, error) in
6768
XCTAssertEqual(placemarks?.count, 0, "forward geocode should return no results for invalid query")
6869
expection.fulfill()
6970
}
71+
XCTAssertNotNil(task)
7072

7173
waitForExpectationsWithTimeout(1) { (error) in
7274
XCTAssertNil(error, "Error: \(error)")
73-
XCTAssertFalse(geocoder.geocoding)
75+
XCTAssertEqual(task?.state, .Completed)
7476
}
7577
}
7678
}

MapboxGeocoderTests/ReverseGeocodingTests.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,19 @@ class ReverseGeocodingTests: XCTestCase {
2525
let geocoder = MBGeocoder(accessToken: BogusToken)
2626
var addressPlacemark: MBPlacemark! = nil
2727
var placePlacemark: MBPlacemark! = nil
28-
geocoder.reverseGeocodeLocation(
28+
let task = geocoder.reverseGeocodeLocation(
2929
CLLocation(latitude: 37.13284000, longitude: -95.78558000)) { (placemarks, error) in
3030
XCTAssertEqual(placemarks?.count, 5, "reverse geocode should have 5 results")
3131
addressPlacemark = placemarks![0]
3232
placePlacemark = placemarks![1]
3333

3434
expectation.fulfill()
3535
}
36+
XCTAssertNotNil(task)
3637

3738
waitForExpectationsWithTimeout(1) { (error) in
3839
XCTAssertNil(error, "Error: \(error)")
39-
XCTAssertFalse(geocoder.geocoding)
40+
XCTAssertEqual(task?.state, .Completed)
4041
}
4142

4243
XCTAssertEqual(addressPlacemark.description, "3099 3100 Rd, Independence, Kansas 67301, United States", "reverse geocode should populate description")
@@ -76,14 +77,15 @@ class ReverseGeocodingTests: XCTestCase {
7677

7778
let expection = expectationWithDescription("reverse geocode execute completion handler for invalid query")
7879
let geocoder = MBGeocoder(accessToken: BogusToken)
79-
geocoder.reverseGeocodeLocation(CLLocation(latitude: 0, longitude: 0)) { (placemarks, error) in
80+
let task = geocoder.reverseGeocodeLocation(CLLocation(latitude: 0, longitude: 0)) { (placemarks, error) in
8081
XCTAssertEqual(placemarks?.count, 0, "reverse geocode should return no results for invalid query")
8182
expection.fulfill()
8283
}
84+
XCTAssertNotNil(task)
8385

8486
waitForExpectationsWithTimeout(1) { (error) in
8587
XCTAssertNil(error, "Error: \(error)")
86-
XCTAssertFalse(geocoder.geocoding)
88+
XCTAssertEqual(task?.state, .Completed)
8789
}
8890
}
8991
}

0 commit comments

Comments
 (0)