Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar detect when user stop type and don't search immediately so detect pause

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?

like image 971
Piero Avatar asked Mar 10 '12 00:03

Piero


1 Answers

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 to performSelector:withObject:afterDelay:; so delayed searches get queued up every text change, and never cancelled.

You could use an NSTimer to delay the invocation of your search, and cancel the timer whenever the text changes: Give your object an NSTimer property or field; in searchBar: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):

// in your class' .h object fields
{
  ...
  NSTimer *searchDelayer; // this will be a weak ref, but adding "[searchDelayer invalidate], searchDelayer=nil;" to dealloc wouldn't hurt
  ...
}

// in the .m

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
  [searchDelayer invalidate], searchDelayer=nil;
  if (YES /* ...or whatever validity test you want to apply */)
    searchDelayer = [NSTimer scheduledTimerWithTimeInterval:1.5
                                                     target:self
                                                   selector:@selector(doDelayedSearch:)
                                                   userInfo:searchText
                                                    repeats:NO];
}

-(void)doDelayedSearch:(NSTimer *)t
{
  assert(t == searchDelayer);
  [self request:searchDelayer.userInfo];
  searchDelayer = nil; // important because the timer is about to release and dealloc itself
}

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...:

// in your class' .h object fields
{
  ...
  NSString *priorSearchText; // this will NOT be a weak ref, so adding "[priorSearchText release]" to dealloc is also required
  ...
}

// in the .m
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
  [NSObject cancelPreviousPerformRequestsWithTarget:self
                                           selector:@selector(request:)
                                             object:priorSearchText];
  [priorSearchText release], priorSearchText = [searchText retain];
  if (YES /* ...or whatever validity test you want to apply */)
    [self performSelector:@selector(request:)
               withObject:searchText
               afterDelay:1.5];
}

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.

like image 143
rgeorge Avatar answered Oct 13 '22 21:10

rgeorge