jeudi 18 février 2016

Text search too slow for large array with search Bar & tableview on iPhone

I have a large array of size around 100000 entries. I am displaying it when user search in UISearchBar and it display filtered data on tableview. However the search bar is too slow while doing dynamic search. i.e I am filtering the table everytime the user puts in a character on search bar. As a result it gives entered text on searchBar as very late response. I just need to wait for it to appear on searchBar.

This is what I am trying to filter my array.

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {

    filtered = data.filter({ (text) -> Bool in
        let tmp: NSString = text
        let range = tmp.rangeOfString(searchText)

        let range = tmp.rangeOfString(searchText, options: [NSStringCompareOptions.CaseInsensitiveSearch ,
        NSStringCompareOptions.AnchoredSearch])
        return range.location != NSNotFound
    })
    if(filtered.count == 0){
        searchActive = false;
    } else {
        searchActive = true;
    }
    self.tableView.reloadData()

}

Aucun commentaire:

Enregistrer un commentaire