Reputation: 1073
There must be a better way to perform search on a remote server database than what I am doing right now.
I have a UISearchController
installed on top of a table view that shows only 30 rows - Upon the user scrolling down to the bottom another 30 rows get loaded up and so forth...
So the the method I am using to perform the search looks like this:
func searchForWriter(searchString: String!) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
Client.sharedInstance.searchForWriterByName(searchString) {(searchResults: [Writer]?, error: NSError?) -> Void in
if let error = error {
NSLog("Error: %@", error.description)
dispatch_async(dispatch_get_main_queue()) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
} else {
dispatch_async(dispatch_get_main_queue()) {
self.filteredWriters = searchResults!
// TODO: - This needs debugging to get the last searched string?
print("searched for: \(searchString)")
self.writersTableView.reloadData()
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
}
}
}
The UISearchResultsUpdating
responds to any change in the searchBar
and hence invoking the above searchForWriter
method.
The problem is when the search results are obtained back from the server, there is no guarantee these will be for the last searched string?
In the matter of fact in almost most of the cases, this method will take longer time get results for a smaller search string rather a longer one... Hence giving completely frustrating search results
Upvotes: 0
Views: 197
Reputation: 9346
In fact you ask several questions here:
1) What you are looking for concerning the amount of displayed rows is called pagination. Look it up for strategies how to do it, but in any case your server has to support it.
2) Concerning the search on each entered character: You should only start searching when at least n characters are entered (n being 3 or so), search results for only one character probably are useless anyway.
3) Add some timer constraint so that the search only starts when the user did not enter anything for 600 ms or so - this avoids lots of calls to the server for unnecessary intermediate results.
Upvotes: 1