ios - 可以显示消息导致崩溃吗?

时间:2012-11-18 22:04:42

标签: ios ios5 nsurlconnection

我有这段代码:

[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从服务器返回时执行。它会在极少数情况下造成崩溃。可能吗?这似乎是一段无害的代码。

谢谢!

3 个答案:

答案 0 :(得分: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];
 });)

很抱歉,我们没有编译过这个,可能在某处有拼写错误。

答案 1 :(得分:1)

如果它进入条件块以显示错误,则不是因为UIAlert,这是因为NSURLConnection遇到错误。我会向控制台输出错误信息,这样你就可以看到错误是什么,当它进入这些罕见的场合并解决NSURLConnection的问题

答案 2 :(得分: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];

     }
}