I want to implement a search functionality one of my iOS application. I have implemented UISearchBar delegates.
Now when user starts searching it makes a service API call.
For example user types:
- A
- Ap
- App
- Appl
- Apple
So total 5 service call is made in very short time. Now i want to cancel all the service call made from 1 to 4 and i want to consider the 5th one as my final result.
I googled for the same and found that i should use NSOperationQueue and cancel the operation when not needed. following is the code i am using for that same.
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if (searchText.length > 0) {
[self filterContentForSearchText:searchText];
}
}
- (void)filterContentForSearchText:(NSString*)searchText {
if ([session isInternetAvailable:NO]) {
if (!downloadQ) downloadQ = [[NSOperationQueue alloc] init];
if (downloadQ) {
[downloadQ cancelAllOperations];
NSLog(@"**********cancelAllOperations**********: %@",downloadQ.name);
}
downloadQ.name = searchText;
NSString *strURL = [NSString stringWithFormat:@"http://192.168.1.192:8080/api/user/find/%@/",searchText];
NSURL *url = [NSURL URLWithString:[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:30];
[urlRequest setHTTPMethod:@"GET"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:downloadQ
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"===> %@",result);
}];
}
}
I need help on this query. Can any one provide me some solution how can i achieve or what would be the best way to implement searching function in iOS app.
Edit With NSURLSEssion
I tried with following code and how i get the response is different sequence.
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
defaultConfigObject.requestCachePolicy = NSURLRequestUseProtocolCachePolicy;
NSURLSession *delegateFreeSession = [NSURLSession sessionWithConfiguration:defaultConfigObject
delegate:nil
delegateQueue:[NSOperationQueue mainQueue]];
NSString *strURL = [NSString stringWithFormat:@"%@/user/find/%@/",LIVE_SERVICE_HOST_URL,searchText];
NSURL *dataURL = [NSURL URLWithString:strURL];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:dataURL];
[urlRequest setHTTPMethod:@"GET"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[[delegateFreeSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response,
NSError *error){
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@===> %@",searchText,result);
}] resume];
Response come in different sequence:
1st response for A 2nd response for Appl 3ed response for Ap 4th response for Apple 5th response for App
So what can i do to over come this behaviour?