2012-09-22 4 views
1

나는 UISearchBarUISearchDisplayController을가집니다. 사용자가 searchBar:textDidChange: 안에 텍스트를 쓰면 웹 서비스 호출을 통해 내 TableView를 필터링합니다. 문제는 웹 서비스에서 결과를 얻을 때까지 GUI가 응답하지 않는다는 것입니다. [self performSelector:@selector(callWebService:) withObject:searchText];을 사용하여 문제를 해결하려고했지만 응답이 없습니다.GUI를 응답하지 않고 웹 서비스 호출을 만드는 방법

편집 : 플 링크 조언에 따라 performSelectorperformSelectorInBackground으로 변경했지만 tableView가 올바르게 필터링되지 않고 '결과 없음'만 표시됩니다. 심지어 tableView:numberOfRowsInSection:이 호출되지 않습니다.

EDIT Again : 'No Results'가 표시되는 이유는 올바른 tableView에서 reloadData을 호출하지 않았기 때문입니다. UISearchDisplayController의 속성은 searchResultsTableView입니다. 그래서 결국 내가 사용한 것은 [self.searchDisplayController.searchResultsTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:false];이었고 이제는 잘 동작합니다.

내가 performSelectorInBackground을 선택했지만 NSURLConnectionsendAsynchronousRequest 메서드를 사용하려고했습니다. AliSoftware의 대답을 참조하십시오.

답변

1

웹을 비동기로 호출해야합니다. 귀하의 경우에는

http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial

, 당신은에 performSelector을 변경할 수 있습니다 performSelectorInBackground

+0

답장을 보내 주셔서 감사합니다. 이제는 performSelectorInBackground로 바꿨지 만 지금은 tableview에 'No Results'가 표시됩니다. – kernix

+0

webservice 호출 함수의 끝에 추가되었습니다. [tableView performSelectorOnMainThread : @selector (reloadData) withObject : nil waitUntilDone : false]; '그러나 여전히 결과를 보이지는 않습니다. – kernix

+0

여기에 코드를 입력하면 더 나은 대답을내는 것이 더 쉬워 질 것입니다. – Shmidt

1
당신은 배경 큐를 만드는 피하거나이 작업자 스레드를 생성로 ( performSelectorInBackground:가하는 일입니다) 네트워크 요청을 수행하는 스레드한다

이를 위해 NSRunLoop에서 요청을 예약하는 것만 큼 효율적이지 않습니다.

스레드를 부여하면 프로세서가 정기적으로 스레드를 활성화하여 데이터가 있는지 확인할 수 있으며 그 스레드를 만드는 것은 상당히 과잉입니다. 실행 루프 (실행 루프 소스)에서 요청을 예약하면 네트워크 인터럽트를 사용하여 소켓에서 들어오는 데이터를 신호로 보내므로 실제로 사용할 수있는 데이터가있을 때만 활성화됩니다.

이렇게하려면 NSURLConnection에서 제공하는 비동기 메서드를 사용하기 만하면됩니다.

  • 하나의 해결책은 NSURLConnection
  • 은 또 다른 더 현대적인 솔루션입니다 (이 그것을 할 수있는 옛날 방식, 다시 iOs3에서 NSURLConnection API에서 사용할 수 있었던 유일한 방법입니다)에서 제공하는 위임 방법을 사용하는 것입니다 NSURLConnection에서 제공하는 블록 API를 사용하는 것이 더 쉽고 사용하기 쉽습니다. 더 URL Loading System Programming Guide에와 NSURLConnection class reference에 읽기

    [NSURLConnection sendAsynchronousRequest:request 
                queue:[NSOperationQueue mainQueue] 
             completionHandler:^(NSURLResponse* response, NSData* receivedData, NSError* error) 
    { 
        // Your code to execute when the network request has completed 
        // and returned a response (or timed out or encountered an error) 
        // This code will execute asynchronously only once the whole data is available 
        // (the rest of the code in the main thread won't be blocked waiting for it) 
    }]; 
    // After this line of code, the request is executed in the background (scheduled on the run loop) 
    // and the rest of the code will continue: the main thread will not be frozen during the request. 
    

.

+0

예, 정확히 블록이 사용되는 종류입니다! – AliSoftware

관련 문제