Search code examples
iphoneobjective-casihttprequestuisearchbaruisearchdisplaycontroller

UISearchDisplayController - wait for N seconds OR for user to press "Search" before conducting search


I have a UISearchDisplayController that I am currently regretting implementing. The problem is that my search controller accesses a web service and then updates a UITableView by calling [tableView reloadData]. Every time a user enters a key, it searches. I've been able to partially relieve the symptoms by requiring at least three characters before calling the ASIHTTPRequest but there must be a better way. Basically I want the UISearchDisplayController to wait for N seconds OR for the user to press "Search" before searching. Here's what I have now:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {

    if (searchString.length > 2) {

        ASIHTTPRequest *request = [IKRequestBuilder requestForIKDogFoodWithQueryString:searchString];
        [request setDelegate:self];
        [request startAsynchronous];
    }   

    return NO;
}

#pragma mark - ASI HTTP Request

- (void)requestStarted:(ASIHTTPRequest *)request {
    //show activity view over table view
}

- (void)requestFinished:(ASIHTTPRequest *)request {
    NSString *response = [request responseString];
    self.dogFoods = [IKJsonFactory buildIKDogFoodArrayWithJSON:response];
    [self.searchDisplayController.searchResultsTableView reloadData];
}

Solution

  • Add an NSTimer as a property on your search delegate.

    In the shouldReloadTableForSearchString:(NSString *)searchString delegate method, instead of sending the request straight away, instead check if you have a timer already, if so cancel it, then start a new timer.

    In the timer's target method, kick off the search.

    This will mean that while they are typing you will keep recreating the timer. When they stop typing the last incarnation of the timer will fire in n seconds. Then you just need to also add the manual kicking off of the search when they press the button. (you could also add a boolean to check if the search has already been started).

    i.e. in your delegates interface

    NSTimer *searchTimer;
    
    @property (nonatomic, retain) NSTimer *searchTimer;
    

    Then in your implementation of the shouldReloadTableForSearchString, instead of firing off the web request, do this:

    if (self.searchTimer) {
        [self.searchTimer invalidate];
        self.searchTimer = nil;
    }
    self.searchTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(searchTimerPopped:) userInfo:nil repeats:FALSE];
    

    Then in your timer target

    -(void) searchTimerPopped:(NSTimer *)sTimer {
        // code to fire off the asynchronous web call to do the search
    }