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

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 回答
1

如果它进入条件块显示错误不是因为UIAlert,那是因为NSURLConnection遇到了错误。我会将错误信息输出到控制台,以便您在遇到这些罕见情况时可以看到错误是什么,并使用 NSURLConnection 解决问题

于 2012-11-18T22:16:51.650 回答
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 回答