Selector with completion, 2 variables and time interval
Asked Answered
P

0

1

Continuing this question

I have this function

func getSearch(completed: @escaping DownloadComplete, searchString: String) {

        let parameters: Parameters = [
            "action" : "search",
            "subaction" : "get",
            "product_name"  : searchString,
            "limit" : "0,30"
        ]
        Alamofire.request(baseurl, method: .get, parameters: parameters).responseJSON { (responseData) -> Void in
            if((responseData.result.value) != nil) {
                let result = responseData.result

                if let dict = result.value as? Dictionary<String, AnyObject>{
                    if let list = dict["products_in_category"] as? [Dictionary<String, AnyObject>] {
                        if self.filteredData.isEmpty == false {
                            self.filteredData.removeAll()
                        }
                        for obj in list {
                            let manPerfumes = Products(productDict: obj)
                            self.filteredData.append(manPerfumes)
                        }
                    }
                }
                completed()
            }
        }
    }

As you can see it handles one completion and one search string.

What i'm trying to do is this

extension SearchViewController: UISearchResultsUpdating {
    
    func updateSearchResults(for searchController: UISearchController) {
        
        if (searchController.searchBar.text?.characters.count)! >= 3 {
                searchString = searchController.searchBar.text!
            NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(self.getSearch(completed:searchString:)), object: nil)
            
            perform(#selector(self.getSearch(completed:searchString:)), with object1: <How to add completion here?>, with object2: searchString, afterDelay: 0.5)    
        } else {
            self.searchResultTable.reloadData()
        }
        
        
    }
}

In this line

perform(#selector(self.getSearch(completed:searchString:)), with object1: <How to add completion here?>, with object2: searchString, afterDelay: 0.5) 

I'm trying to figure out how to add the completion on the first object and why i cant have 2 object and afterDelay??

Is it possible for me to have those 2? And if yes, how can i extend the Perform class?

Peasecod answered 24/2, 2017 at 18:30 Comment(6)
Use DispatchQueue asyncAfter instead of perform...Tagliatelle
@Tagliatelle it should be the same i guess as the perform. right?Peasecod
@Tagliatelle as i test it yes it does wait the time that i've told it to wait. Although is it a good practise to do so with asyncAfter?Peasecod
Using DispatchQueue is much easier, cleaner, and safer than using perform.Tagliatelle
Ok as i told you i tried it and it works smoothly without any memory issue!Peasecod
But you then asked me if it is good practice. So I answered that question. Yes it is.Tagliatelle

© 2022 - 2024 — McMap. All rights reserved.