Description:
I'm currently using the following code to see if the user has stopped typing in the searchBar. I would like to cancel it everytime the user immediately starts typing after 0.5
seconds.
Code:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
// your function here
}
Question:
How do I cancel DispatchQueue.main.asyncAfter
if the user starts typing again in Swift3
?
What I've tried:
I previously tried implementing :
NSObject.cancelPreviousPerformRequests(withTarget: self)
self.perform(Selector(("searchForText:")), with: searchString, afterDelay: 0.5)
However the delay does not seem to work properly.
More code:
//In class SearchViewController: UITableViewController, UISearchResultsUpdating
func updateSearchResults(for searchController: UISearchController) {
let searchString: String = searchController.searchBar.text!
//This is what I previously tried.. which doesn't work...
//NSObject.cancelPreviousPerformRequests(withTarget: self)
//self.perform(Selector(("searchForText:")), with: searchString, afterDelay: 0.5)
//A struct with the first example code shown above.
Utils.Dispatch.delay(secondsToDelay: 1){
print("1 second has passed ! " + searchString)
}
}