Swift 3 中的实时搜索限制 [英] Live search throttle in Swift 3

查看:25
本文介绍了Swift 3 中的实时搜索限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Swift 在我的 PHP API 上进行实时搜索.到目前为止,我已经完成了这件事.

I'm trying to live search at my PHP API with Swift. Until now i've done this thing.

 var filteredData = [Products]()


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


extension SearchViewController: UISearchResultsUpdating {

    func updateSearchResults(for searchController: UISearchController) {

        if (searchController.searchBar.text?.characters.count)! >= 3 {
                    self.getSearch(completed: {
                    self.searchResultTable.reloadData()

                    self.searchResultTable.setContentOffset(CGPoint.zero, animated: true)
                }, searchString: searchController.searchBar.text!)


        } else {
            self.searchResultTable.reloadData()
        }


    }
}

表格视图正在使用 filteredData 进行更新.我如何限制搜索以便让我们说用户何时写

And the table view is being updated with the filteredData. How can i throttle the search so lets say when the user writes

"example" -> shows the results with example
then he erase the "le" -> 
"examp" -> if the previous request is not completed, cancel it -> make request for "examp" and show the data in table view!

附言从我发现的另一个答案

P.S. from another answer i found

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    // to limit network activity, reload half a second after last key press.
    NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(self.reload), object: nil)
    self.perform(#selector(self.reload), with: nil, afterDelay: 0.5)
}
func reload() {
    print("Doing things")
}

虽然如果我尝试用我的函数替换self.reload",我会收到一个错误无法将类型 () 的值转换为预期的参数类型选择器

Although if I try to replace "self.reload" with my function, I get an error cannot convert value of type () to expected argument type selector

推荐答案

你的错误是因为你可能忘记了 #selector() 部分.

Your error was because you probably forgot the #selector() part.

它应该是这样的:

func searchBar() { 
    NSObject.cancelPreviousPerformRequests(withTarget: self,    
                                           selector: #selector(self.getSearch(completed:searchString:)), 
                                           object: nil) 

    perform(#selector(self.getSearch(completed:searchString:)), 
            with: nil, afterDelay: 0.5) }

您收到错误是因为您没有将函数包含在 #selector

You get the error because you didn't enclose your function in #selector

现在,至于参数,这是一个函数:

Now, as for the arguments, here's a function for that:

perform(#selector(getSearch:completion:searchString), with: <some completion>, with: "search string")

这篇关于Swift 3 中的实时搜索限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆