0

私はこのコードを持っています:

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {         
     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

     if ( error != nil )
     {
         // Display a message to the screen.
         UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"There was a server error getting your business plan. We use a remote server to backup your work." 
                    message:@"Please make sure your phone is connected to the Internet. If the problem persists, please let us know about this." 
                delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

         [message show];

ただし、NSUrlConnection がサーバーから返されたときに実行されます。まれにクラッシュが発生します。出来ますか?無害なコードのように思えます。

ありがとう!

4

3 に答える 3

1

エラーを表示するために条件付きブロックに入る場合は、UIAlert が原因ではなく、NSURLConnection でエラーが発生したためです。エラー情報をコンソールに出力して、これらのまれな状況になったときにエラーが何であるかを確認し、NSURLConnection で問題を解決できるようにします。

于 2012-11-18T22:16:51.650 に答える
1

NSURLConnection は奇妙なスレッドで結果を返していますか? わかりませんが、UIAlertView は UI から始まるため、UI スレッドでのみ動作するように意図されているのではないかと思います。

 (dispatch_async(dispatch_get_main_queue(), ^{ 
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"There was a server error getting your business plan. We use a remote server to backup your work." 
                message:@"Please make sure your phone is connected to the Internet. If the problem persists, please let us know about this." 
            delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

     [message show];
 });)

申し訳ありませんが、これをコンパイルしていません。おそらくどこかにタイプミスがあります。

于 2012-11-18T22:10:41.387 に答える
0

問題は、メイン スレッドで alertView を表示していないことです。すべての UI 関連のコードは、メイン スレッドで動作する必要があります。

別のスレッドで alertView を表示したときに、同様のクラッシュが発生しました。

次のようにメソッドを変更する必要があります。

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {         
     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

     if ( error != nil )
     {
         // Display a message to the screen.
        dispatch_async(dispatch_get_main_queue(), ^{

          UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"There was a server error getting your business plan. We use a remote server to backup your work." 
                    message:@"Please make sure your phone is connected to the Internet. If the problem persists, please let us know about this." 
                delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

          [message show];
       });
     }
}

または次のように変更します。

 [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {         
     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

     if ( error != nil )
     {
         // Display a message to the screen.
          UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"There was a server error getting your business plan. We use a remote server to backup your work." 
                    message:@"Please make sure your phone is connected to the Internet. If the problem persists, please let us know about this." 
                delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
         [message performSelectorOnMainThread:@selector(show) withObject:nil waitUntillDone:NO];

     }
}
于 2012-11-19T04:16:17.810 に答える