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

Fix data task handler return queue issue #182

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions MapboxGeocoder/MBGeocoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,18 @@ open class Geocoder: NSObject {

request.setValue(userAgent, forHTTPHeaderField: "User-Agent")
return URLSession.shared.dataTask(with: request) { (data, response, error) in

let returnQueue = DispatchQueue.main

guard let data = data else {
if let e = error as NSError? {
errorHandler(e)
returnQueue.async {
errorHandler(e)
}
} else {
let unexpectedError = NSError(domain: MBGeocoderErrorDomain, code: -1024, userInfo: [NSLocalizedDescriptionKey : "unexpected error", NSDebugDescriptionErrorKey : "this error happens when data task return nil data and nil error, which typically is not possible"])
errorHandler(unexpectedError)
returnQueue.async {
errorHandler(unexpectedError)
}
}
return
}
Expand All @@ -286,12 +291,12 @@ open class Geocoder: NSObject {
// Check if any of the batch geocoding queries failed
if let failedResult = result.first(where: { $0.message != nil }) {
let apiError = Geocoder.descriptiveError(["message": failedResult.message!], response: response, underlyingError: error as NSError?)
DispatchQueue.main.async {
returnQueue.async {
errorHandler(apiError)
}
return
}
DispatchQueue.main.async {
returnQueue.async {
completionHandler(data)
}
} catch {
Expand All @@ -301,18 +306,18 @@ open class Geocoder: NSObject {
// Check if geocoding query failed
if let message = result.message {
let apiError = Geocoder.descriptiveError(["message": message], response: response, underlyingError: error as NSError?)
DispatchQueue.main.async {
returnQueue.async {
errorHandler(apiError)
}
return

}
DispatchQueue.main.async {
returnQueue.async {
completionHandler(data)
}
} catch {
// Handle errors that don't return a message (such as a server/network error)
DispatchQueue.main.async {
returnQueue.async {
errorHandler(error as NSError)
}
}
Expand Down