i’m searching implementing a UISearchbar that retrieve information from a url, and with the default method:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
i can detect immediately when the text change and perform the fetch of url, but in this way, the text type is slow because the iPhone is searching the url, so i want start the fetching of the url when the user is stop writing for some second, so i want detect the pause of the typing to refresh the table view retrieving the information by the url. i have found a older post of this request and i have tried a solution that for me don’t work:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(request:) object:searchText];
//.....
[self performSelector:@selector(request:) withObject:searchText afterDelay:1.5];
}
-(void)request:(NSString *)myString
{
NSLog(@"%@",myString);
}
in this way when i’m typing the request method is not called but when i stop typing it’s called for every character i type, so is the same of the default method, i wrong something? or the implementation isn’t correct?
It looks like the provided solution isn’t working because the “searchText” argument to
cancelPreviousPerformRequestsWithTarget:selector:object:doesn’t match the “searchText” argument to the previous call toperformSelector:withObject:afterDelay:; so delayed searches get queued up every text change, and never cancelled.You could use an
NSTimerto delay the invocation of your search, and cancel the timer whenever the text changes: Give your object anNSTimerproperty or field; insearchBar:textDidChange:cancel any existing timer, then create and schedule a new one. The target of the timer should call your search method.Something like (off the top of my head):
Some programmers might be less cavalier about weak references than I’m being here.
[edited to add:]
Here’s how you might do it if you were set on using cancelPreviousPerformRequestsWithTarget…:
I don’t think I’ve ever used cancelPreviousPerformRequestsWithTarget:… for real, so I don’t know if it hides any surprises. If you have trouble, add NSLogs, look for delayed searches not getting cancelled when they should.