2016-09-20 53 views
0

我想實現我的iOS應用程序的搜索功能之一。我已經實現了UISearchBar委託。如果有新的請求取消請求

現在,當用戶開始搜索時,它會進行服務API調用。

例如用戶類型:

  1. 一個
  2. 應用
  3. 申請
  4. 蘋果

所以總共5服務呼叫在很短的時間內做出。 現在我想取消從1到4的所有服務呼叫,我想把第五個作爲我的最終結果。

我使用相同的搜索引擎,發現我應該使用NSOperationQueue並在不需要時取消操作。以下是我正在使用的代碼。

- (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); 
           }]; 
    } 
} 

我需要關於此查詢的幫助。任何人都可以爲我提供一些解決方案我如何實現或者什麼是在iOS應用中實現搜索功能的最佳方式。

編輯隨着NSURLSEssion

我試着用下面的代碼,以及如何我得到的反應是不同的序列。

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]; 

響應有不同的順序:

爲用於爲App

蘋果 5響應鴨 第四響應申請 3ED響應的 第二響應1響應

所以,我能做些什麼過來這種行爲?

+0

使用NSURLSession所以,當你需要做的,你可以取消的請求。看看:https://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started –

+0

@霍亞:我嘗試使用NSURLSession,但我從服務器以不同的順序得到響應。請查看我編輯的問題。你能建議我怎麼解決這個問題? – iCoder86

+0

查看我發佈的鏈接中的「查詢曲目」會話。他們解決與你的相同的用例。關鍵的想法是重用相同的dataTask,每次更改搜索文本時,它會取消當前的dataTask(如果啓動)並重新創建新的。 –

回答

0

替代的解決方案:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { 
    if ([searchText length] <= 0) { 
     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(filterContentForSearchText) object:self]; 
     [self performSelector:@selector(filterContentForSearchText) withObject:self afterDelay:0.5]; 
    } 
} 

參考來源:Here