2

I have implemented a AutoSuggest API (Similar to Google Search). API gives list of suggestion for character typed in search bar. Eg: While typing on Google search, it show list of suggestion on bottom of search bar.

Any good approach or design pattern we can use for calling this API on each char type.

Current implementation is : 1. char type on search bar 2. create an NSURLRequest and pass it to NSURLConnection object 3. Parse response and show suggestion. 4. Again char type just Cancel the NSURLConnection before generate a request and passing to NSURLConnection.

My concern is what could be the good approach for implementing this.

Thanks in Advance. Regards, Ruyam

ruyamonis346
  • 357
  • 3
  • 15

2 Answers2

0

You obviously can't wait until you get a response. By the time you get a response for the first letter, I have typed three more letters :-)

First check whether giving any choices after typing one or two letters makes sense (your decision). Then do some measurements to check whether cancelling previous requests makes sense. You might never get any suggestions if you cancel suggestions all the time. And once a NSURLConnection has started running, cancelling it might not save anything (once the request is sent to the server, the server will reply, the only difference is whether you ignore it).

I'd send out requests after every letter, avoiding sending the same request twice after the user deletes a character. As responses come in, cache them but only process them if they are still relevant (reply might come in after the user switched to another view!) All requests must run in the background obviously. Probably cache the last dozen requests. And check whether the user is on WiFi or 3G or that API might be expensive.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • 1
    Thanks for the reply! Agree on not to send the request while deleting the character. But caching last dozen of request is somewhat complex pattern. Still struggling to use good design pattern for this – ruyamonis346 Jul 02 '14 at 10:42
0

You can use approach of this kind using NSTimer :

NSTimer *myTimer;

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

    if (myTimer)
    {
        if ([myTimer isValid])
        {
            [myTimer invalidate];
        }
        myTimer=nil;
    }
    myTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(searchPlaces:) userInfo:nil repeats:NO];
}

- (void)searchPlaces:(id)sender {

}
Rajan Balana
  • 3,775
  • 25
  • 42