내 iOS 응용 프로그램 중 하나의 검색 기능을 구현하고 싶습니다. UISearchBar 대리자를 구현했습니다.새로운 요청이있을 경우 요청 취소
사용자가 검색을 시작하면 서비스 API 호출이 생성됩니다. 예를 들어 사용자 유형의
:
그래서 총 5 서비스 호출이 매우 짧은 시간에 만들어
- AP = 연합 뉴스
- 앱. 이제 1에서 4까지의 서비스 콜을 모두 취소하고 5 번째 것을 최종 결과로 생각하고 싶습니다.
나는 그걸 봤 거든 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
나는 다음과 같은 코드를 어떻게 얻을 응답이 다른 순서입니다 함께했습니다. 앱
애플 5 응답 AP = 연합 뉴스 4 응답 출원 3ED 응답에 대한 2 응답1 응답
그래서 내가 무엇을 할 수
: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];
응답은 다른 순서로 와서 이 행동을 끝내기 위해?
사용 NSURLSession :에서
참조. 다음을보십시오 : https://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started –
@Hoa : NSURLSession을 시도했지만 다른 순서로 서버로부터 응답을 받았습니다. 제 수정 된 질문을 검토하십시오. 그 문제를 해결하기 위해 내가 무엇을 할 수 있는지 제안 해 주시겠습니까? – iCoder86
내가 게시 한 링크의 "트랙 쿼리"세션을 살펴보십시오. 귀하와 동일한 유즈 케이스를 해결합니다. 핵심 아이디어는 동일한 데이터 태스크를 다시 사용하는 것입니다. 검색 텍스트를 변경할 때마다 현재 데이터 태스크 (실행중인 경우)를 취소하고 새 데이터 테이블을 다시 작성합니다. –