-(IBAction) webRequest;
{
response = [[NSMutableData alloc] init];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]
initWithURL: [NSURL URLWithString:kResourcesURL]
cachePolicy: NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval: 10];
[theRequest setValue:@"application/json" forHTTPHeaderField:@"accept"];
[theRequest setValue:strToken forHTTPHeaderField:@"token"];
//show network activity indicator
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
response = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlresponse error:&nserror];
NSString *strResponse = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
SBJsonParser *sbjasonparser = [[SBJsonParser alloc] init];
arrResponse = [sbjasonparser objectWithString:strResponse error:nil];
//hide network activity indicator
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
私のコードに何か問題がありますか?または同期化された要求によって差異が生じますか? または、AcivityIndicatorビューを使用する必要がありますか?networkActivityIndicatorVisibleが機能しない(同期要求)
メインスレッドで同期ネットワーク要求(または時間のかかる作業)を実行しないでください。そのタスクが完了するまで、あなたのアプリは応答しません。これはユーザーエクスペリエンスが悪いだけでなく、OSが応答時間が長すぎるとアプリが終了することがあります。実際のネットワーク環境下でこの質問に示されている同期アプローチを試みるとすぐに、アプリケーションはロックされてクラッシュしたように見えます。 – Jonah
@Jonah:nextviewのレイアウトが現在のリクエストに対する応答に完全に依存している場合はどうですか? – viral
@codeCube: 'NSURLConnectionDelegate'メソッドを使うことができます。バックグラウンドスレッドまたは 'NSOperationQueue'ルートを選択した場合は、ビュー設定コードを呼び出すために' -performSelectorOnMainThread:withObject:waitUntilDone: 'を使用してください。 iOS 5専用のコードがあれば、ビュー構成を '+ sendAsynchronousRequest:queue:completionHandler:'の補完ブロックに入れてください。 – Costique