简体   繁体   English

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

[英]ios - can showing a message cause a crash?

I have this code: 我有这个代码:

[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];

but it executes when a NSUrlConnection returns from the server. 但是当NSUrlConnection从服务器返回时它会执行。 It creates a crash on rare occasions. 它会在极少数情况下造成崩溃。 Is it possible? 可能吗? It seems like such a harmless piece of code. 这似乎是一段无害的代码。

Thanks! 谢谢!

Is the NSURLConnection returning a result on some odd thread? NSURLConnection是否在一些奇怪的线程上返回结果? I don't know, but I suspect that UIAlertView is only meant to work on the UI thread, since it starts with UI. 我不知道,但我怀疑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];
 });)

Sorry, haven't compiled this, there's probably a typo in there somewhere. 对不起,还没编译过这个,某处可能有拼写错误。

If it's entering the conditional block to display the error it's not because of the UIAlert, it's because the NSURLConnection encountered an error. 如果它进入条件块以显示错误,那不是因为UIAlert,这是因为NSURLConnection遇到错误。 I would output to the console the error information so that you can see what the error is when it gets into these rare occasions and solve the issue with the NSURLConnection 我会向控制台输出错误信息,以便您可以看到错误是什么,当它进入这些罕见的场合并解决NSURLConnection的问题

The issue is that, you are not displaying the alertView in the main thread. 问题是,您没有在主线程中显示alertView。 All UI related code is need to be work on main thread. 所有与UI相关的代码都需要在主线程上工作。

I got a similar crash when I displayed the alertView on another thread. 当我在另一个线程上显示alertView时,我遇到了类似的崩溃。

You need to change the method like: 您需要更改方法,如:

[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];
       });
     }
}

or change it like: 改变它:

 [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];

     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM