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 #1725: Update search suggestions list on main thread #1728

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -40,44 +40,46 @@ class SearchSuggestClient {
}

request = session.dataTask(with: url!, completionHandler: { data, response, error in
if let error = error {
return callback(nil, error as NSError?)
}

let responseError = NSError(domain: SearchSuggestClientErrorDomain, code: SearchSuggestClientErrorInvalidResponse, userInfo: nil)

if let response = response as? HTTPURLResponse {
if !(200..<300).contains(response.statusCode) {
return callback(nil, responseError)
DispatchQueue.main.async {
if let error = error {
return callback(nil, error as NSError?)
}
}

guard let data = data else {
return callback(nil, responseError)
}

do {
let result = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)

// The response will be of the following format:
// ["foobar",["foobar","foobar2000 mac","foobar skins",...]]
// That is, an array of at least two elements: the search term and an array of suggestions.
guard let array = result as? NSArray else {
return callback(nil, responseError)
let responseError = NSError(domain: SearchSuggestClientErrorDomain, code: SearchSuggestClientErrorInvalidResponse, userInfo: nil)

if let response = response as? HTTPURLResponse {
if !(200..<300).contains(response.statusCode) {
return callback(nil, responseError)
}
}

if array.count < 2 {
guard let data = data else {
return callback(nil, responseError)
}

let suggestions = array[1] as? [String]
if suggestions == nil {
return callback(nil, responseError)

do {
let result = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)

// The response will be of the following format:
// ["foobar",["foobar","foobar2000 mac","foobar skins",...]]
// That is, an array of at least two elements: the search term and an array of suggestions.
guard let array = result as? NSArray else {
return callback(nil, responseError)
}

if array.count < 2 {
return callback(nil, responseError)
}

let suggestions = array[1] as? [String]
if suggestions == nil {
return callback(nil, responseError)
}

callback(suggestions!, nil)
} catch {
return callback(nil, error as NSError?)
}

callback(suggestions!, nil)
} catch {
return callback(nil, error as NSError?)
}
})
request?.resume()
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.