For those that have time to test code, I'll post my current solution that is untested. When I have time to try it, I'll edit the post.
private var operationQueue: OperationQueue!
private var mainAsyncQueue: DispatchQueue?
override func viewDidLoad() {
print("ViewDidLoad of SearchViewController called")
self.operationQueue = OperationQueue()
self.currentTime = DispatchTime.now()
}
// MARK: UISearchResultsUpdating
func updateSearchResults(for searchController: UISearchController) {
let searchStringRaw: String = searchController.searchBar.text!
let searchString = searchStringRaw.trimmingCharacters(in: .whitespacesAndNewlines)
guard searchString.characters.count > 0 else {
return
}
print("Search string: \(searchString)")
self.operationQueue.cancelAllOperations()
//Put this in Utils.Dispatch.Delay
self.mainAsyncQueue = DispatchQueue(label: "search.operation." + String(describing: DispatchTime.now()), qos: .default, attributes: DispatchQueue.Attributes.concurrent)
let time = DispatchTime.now()
self.currentTime = time
self.mainAsyncQueue!.asyncAfter(deadline: time + 1){
guard self.currentTime == time else {
return
}
let tempOperation = BlockOperation(block:{
if let nsurl: URL = Utils.Url.generate(Constants.Url.Search, options: "&p=1&n=20&q="+searchString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!){
//Download data and handle response
} else {
print("Something went wrong...")
}
})
self.operationQueue.addOperation(tempOperation)
}
}