【问题标题】:ios - can showing a message cause a crash?ios - 显示消息会导致崩溃吗?
【发布时间】:2012-11-18 22:04:41
【问题描述】:

我有这个代码:

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

谢谢!

【问题讨论】:

  • 您的消息内容是从NSURLConnection 中提取的吗?
  • 这段代码在哪里?它是否放置在NSURLConnectionDelegate 指定的委托方法中?如果是这样,问题就在于此。如果您可以发布更多示例代码,我们将更容易为您提供帮助。
  • @esqew 不,内容只是纯文本。
  • 刚刚发布了一个更大的代码时钟。

标签: ios ios5 nsurlconnection


【解决方案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];
 });)

抱歉,这里没有编译,可能有错别字。

【讨论】:

  • 代码在 NSUrlConnection 调用后执行的块中。不建议在那里显示该消息吗?我应该如何正确地向用户展示内容?
  • 对不起,这里写的不够小。看下一个答案!
【解决方案2】:

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

【讨论】:

  • Crashalytics 提供了有关错误的额外信息:异常类型:SIGSEGVCode:SEGV_ACCERR
  • __36-[BusinessController viewDidAppear:]_block_invoke_0
  • 但是我将如何重构代码以使其更安全且不会崩溃?
  • 当它出错时,我会输出由localizedDescriptionlocalizedFailureReason 返回的字符串,如 NSError 类参考中所指定的,以深入了解 NSURLConnection 失败的原因。参考:developer.apple.com/library/mac/#documentation/Cocoa/Reference/…
【解决方案3】:

问题是,您没有在主线程中显示 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];

     }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多