Ios 当自动完成搜索从数据库中获取结果时,搜索速度会变慢并占用内存
你好,我已经在我的应用程序上实现了自动完成搜索。我在mysql数据库和应用程序中存储了城市,当用户键入任何字符或单词时,应用程序将从数据库中获取结果并显示出来。我遇到的问题是,数据库中存储了1000多个城市,当用户输入一个字符时,我的应用程序键盘有点卡住,在获取结果并显示结果时占用了大量内存。有没有更好的方法来实现这种功能。请查看我的代码,让我知道我应该在代码中做哪些更改Ios 当自动完成搜索从数据库中获取结果时,搜索速度会变慢并占用内存,ios,swift,uisearchcontroller,Ios,Swift,Uisearchcontroller,你好,我已经在我的应用程序上实现了自动完成搜索。我在mysql数据库和应用程序中存储了城市,当用户键入任何字符或单词时,应用程序将从数据库中获取结果并显示出来。我遇到的问题是,数据库中存储了1000多个城市,当用户输入一个字符时,我的应用程序键盘有点卡住,在获取结果并显示结果时占用了大量内存。有没有更好的方法来实现这种功能。请查看我的代码,让我知道我应该在代码中做哪些更改 func updateSearchResultsForSearchController(searchController:
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()
}
func updateSearchResultsForSearchController(searchController:UISearchController){
filterTableData.removeAll(keepCapacity:false)
让searchWord=searchController.searchBar.text!
getCityNamesFromServer(searchWord)
让searchPredict=NSPredicate(格式:“SELF-CONTAINS[c]@”,searchController.searchBar.text!)
对于变量i=0;i
完整代码:
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
}
}
}
}
class CityTableViewController:UITableViewController、UISearchResultsUpdate{
var dict=NSDictionary()
var filterTableData=[String]()
var resultSearchController=UISearchController()
var newTableData=[String]()
重写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
返回控制器
})()
self.tableView.reloadData()
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
if(self.resultSearchController.active){
返回self.filterTableData.count
}否则{
返回指令计数
}
}
重写func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
让cell=tableView.dequeueReusableCellWithIdentifier(“cell”,forIndexPath:indexPath)作为!CountryTableViewCell
if(self.resultSearchController.active){
cell.citynamelab.text=filterTableData[indexPath.row]
返回单元
}否则{
cell.citynamelab.text=((self.dict[“\(indexath.row)”]作为?NSDictionary)![“City”]作为?NSDictionary)![“name”]作为?NSString)!作为String
返回单元
}
}
func updateSearchResultsForSearchController(searchController:UISearchController){
filterTableData.removeAll(keepCapacity:false)
让searchWord=searchController.searchBar.text!
getCityNamesFromServer(searchWord)
让searchPredict=NSPredicate(格式:“SELF-CONTAINS[c]@”,searchController.searchBar.text!)
打印(“searchPredict是\(searchController.searchBar.text!))
对于变量i=0;i
限制数据量的最佳方法是在查询中添加limit子句。SQL查询示例:
SELECT name FROM cities WHERE name like '%something%';
将此更改为:
SELECT name FROM cities WHERE name like '%something%' LIMIT 10;
从用户的角度来看,返回1000行是没有意义的,您无论如何都无法显示它,因此您必须找到一个适合屏幕的数字。在任何其他按键后,您可以使用更新的搜索字符串重复查询。好的,我遇到的一个小问题是,它多次显示相同的城市。是否有任何方法仅显示不同的值。我的意思是,从数据库中没有重复的条目,但在tableview中,它多次显示同一个城市。我想您需要在两次运行之间重新初始化数组。我这样做了,但它没有显示任何记录。var i=0时为空;i