gpt4 book ai didi

ios - 自动完成搜索在从数据库中获取结果时变慢并占用内存

转载 作者:行者123 更新时间:2023-11-28 06:48:25 27 4
gpt4 key购买 nike

你好,我已经在我的应用程序上实现了自动完成搜索。当用户键入任何字符或单词时,我将城市存储在我的 mysql 数据库和应用程序中,应用程序从数据库中获取结果并显示它。我遇到的问题是数据库中存储了 1000 多个城市,当用户说输入一个字符时,我的应用程序键盘有点卡住了,它在获取结果并显示时占用了大量内存。有没有更好的方法来实现这种功能。请查看我的代码,让我知道我应该对我的代码进行哪些更改

func updateSearchResultsForSearchController(searchController: UISearchController) {
filterTableData.removeAll(keepCapacity: false)
let searchWord = searchController.searchBar.text!

getCityNamesFromServer(searchWord)

let searchPredict = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)

for var i = 0; i < self.dict.count; i++ {
let cityname = (((self.dict["\(i)"] as?NSDictionary)!["City"] as?NSDictionary)!["name"] as?NSString)! as String
newTableData.append(cityname)
}

let array = (newTableData as NSArray).filteredArrayUsingPredicate(searchPredict)
print("array is\(array)")
filterTableData = array as! [String]
self.tableView.reloadData()
}

完整代码:

class  CityTableViewController: UITableViewController, UISearchResultsUpdating {
var dict = NSDictionary()
var filterTableData = [String]()
var resultSearchController = UISearchController()


var newTableData = [String]()


override func viewDidLoad() {
super.viewDidLoad()



self.resultSearchController = ({

let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller


})()

self.tableView.reloadData()
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


if(self.resultSearchController.active){

return self.filterTableData.count
}else {

return dict.count
}



}



override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CountryTableViewCell

if(self.resultSearchController.active){

cell.cityNameLabel.text = filterTableData[indexPath.row]
return cell

}else{

cell.cityNameLabel.text = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["City"] as?NSDictionary)!["name"] as?NSString)! as String
return cell
}



}

func updateSearchResultsForSearchController(searchController: UISearchController) {
filterTableData.removeAll(keepCapacity: false)
let searchWord = searchController.searchBar.text!

getCityNamesFromServer(searchWord)

let searchPredict = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
print("searchPredict is \(searchController.searchBar.text!)")

for var i = 0; i < self.dict.count; i++ {
let cityname = (((self.dict["\(i)"] as?NSDictionary)!["City"] as?NSDictionary)!["name"] as?NSString)! as String
newTableData.append(cityname)
}

let array = (newTableData as NSArray).filteredArrayUsingPredicate(searchPredict)
print("array is\(array)")
filterTableData = array as! [String]
self.tableView.reloadData()
}






func getCityNamesFromServer(searchWord:String){


let url:String = "http://localhost/"
let params = ["city":searchWord]



ServerRequest.postToServer(url, params: params) { result, error in

if let result = result {
print(result)

self.dict = result

}
}

}

}

最佳答案

限制返回数据量的最佳方法是在查询中添加 LIMIT 子句。示例 SQL 查询:

SELECT name FROM cities WHERE name like '%something%';

将此更改为:

SELECT name FROM cities  WHERE name like '%something%' LIMIT 10;

从用户的角度来看,返回 1000 行没有意义,无论如何您都无法显示它,因此您必须想出一个适合屏幕的数字。在任何额外的按键之后,您可以使用更新的搜索字符串重复查询。

关于ios - 自动完成搜索在从数据库中获取结果时变慢并占用内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35597439/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com