How to cancel DispatchQueue.main.asyncAfter(deadline: time) in Swift3?

kemicofa ghost picture kemicofa ghost · Oct 4, 2016 · Viewed 10.3k times · Source

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)
    }
}

Answer

kemicofa ghost picture kemicofa ghost · Nov 28, 2016

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)
    }

}