1

と がUISearchBarありUISearchDisplayControllerます。ユーザーが内部にテキストを書き込むと searchBar:textDidChange:、Web サービス呼び出しを行って TableView をフィルター処理します。問題は、Web サービスから結果を取得するまで GUI が応答しなくなることです。を使用して解決しようとしました[self performSelector:@selector(callWebService:) withObject:searchText];が、まだ応答しません。

編集: Flink のアドバイスに従って、 に変更performSelectorしましperformSelectorInBackgroundたが、tableView が正しくフィルター処理されず、「結果なし」のみが表示されます。もtableView:numberOfRowsInSection:呼び出されません。

再度編集:「結果がありません」と表示された理由はreloadData、正しい tableView を呼び出していないためです。UISearchDisplayControllerという名前のプロパティがありますsearchResultsTableView。結局、私が使用したのは[self.searchDisplayController.searchResultsTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:false];、今では正常に動作します。

performSelectorInBackgroundを選択しましたが、おそらくsendAsynchronousRequeston メソッドを使用する必要があったことに注意してNSURLConnectionください-AliSoftwareの回答を参照してください。

4

2 に答える 2

1

Web 呼び出しを非同期にする必要があります。

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

あなたの場合、次のように変更できperformSelectorますperformSelectorInBackground

于 2012-09-22T11:11:04.130 に答える
1

ネットワーク リクエストを実行するためにバックグラウンド キューまたはスレッドを作成することは避ける必要があります (これは実際にperformSelectorInBackground:行われていることです) NSRunLoop

スレッドを専用化すると、プロセッサは定期的にスレッドをアクティブにして、データがあるかどうかを確認します。そのためのスレッドを作成するのは非常にやり過ぎです。実行ループで (実行ループ ソースとして) 要求をスケジュールすると、ネットワークの中断を使用してソケットからの着信データが通知されるため、実際のデータが利用可能な場合にのみアクティブになります。

これを行うには、 が提供する非同期メソッドを使用するNSURLConnectionだけです。

  • 1 つの解決策は、によって提供されるデリゲート アプローチを使用することNSURLConnectionです (これは古い方法であり、NSURLConnectioniOS3 の API で利用できた唯一の方法です)。
  • もう 1 つの最新のソリューションは、NSURLConnection使用とコーディングが簡単なブロック API を使用することです。

    [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.
    

詳細については、URL ローディング システム プログラミング ガイドNSURLConnectionクラス リファレンスを参照してください。

于 2012-09-22T11:32:35.740 に答える