Detect Pause when user stop type in UISearchBar

ios, ipad, iphone, nstimer, uisearchbar

Solution

Please write this line in .h file

NSTimer *myTimer;

and then do it in .m file

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{

    NSLog(@"Timer=%@",myTimer);
    if (myTimer)
    {
        if ([myTimer isValid])
        {
            [myTimer invalidate];
        }
        myTimer=nil;
    }
    myTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(OnTextChange) userInfo:nil repeats:NO];

}

Problem

I have the following UISearchbar code: ``` - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { NSTimer *myTimer; NSLog(@"Timer=%@",myTimer); if (myTimer) { if ([myTimer isValid]) { [myTimer invalidate]; } myTimer=nil; } myTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(OnTextChange) userInfo:nil repeats:NO]; } ``` after writing above code my log shows `Timer=null`, `OnTextChange` is method which fetches data from Url. Thanks in advance !!

Original source