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

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


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.


Move this line:[self performSelector:@selector(request:) withObject:searchText afterDelay:1.5];out of your textDidChange method and put it in the text did begin editing method:- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar