Selector with completion, 2 variables and time int

2019-06-05 10:56发布

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?

0条回答
登录 后发表回答