简体   繁体   English

iOS UIAlertView提供EXC_BAD_ACCESS

[英]iOS UIAlertView giving EXC_BAD_ACCESS

I have searched for awhile and tried many things, but I cannot get this error to go away. 我搜索了一段时间并尝试了很多方法,但是我无法摆脱这个错误。 I have a table that is refreshed by a pull down method. 我有一个通过下拉方法刷新的表。 I also use Apple's Reachability to determine internet connectivity. 我还使用Apple的可达性来确定Internet连接。 When I run my application WITH internet, and then while the app is running, shut the internet off, my app crashes trying to display a UIAlertView. 当我使用Internet运行我的应用程序时,然后在应用程序运行时,关闭Internet,我的应用程序崩溃,试图显示UIAlertView。 Although, when I start my app WITHOUT internet and then turn on internet while the app is running and turn it back off, the app does not crash and everything works as it should. 虽然,当我在没有Internet的情况下启动我的应用程序,然后在运行该应用程序时打开Internet并将其关闭时,该应用程序不会崩溃,并且一切正常。 Any help would be greatly appreciated. 任何帮助将不胜感激。

The error occurs on the [message show] line. 该错误发生在[message show]行上。

Edited code: 编辑代码:

- (void)loadCallList {

NSURL *theURL = [NSURL URLWithString:@"http://www.wccca.com/PITS/"];

NSURLRequest *theRequest = [NSURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if (theConnection) {

    NSLog(@"Reachable");

    self.headerHeight = 45.0;

    NSData *data = [[NSData alloc] initWithContentsOfURL:theURL];
    xpathParser = [[TFHpple alloc] initWithHTMLData:data];
    NSArray *elements = [xpathParser searchWithXPathQuery:@"//input[@id='hidXMLID']//@value"];

        if (elements.count >= 1) {

            TFHppleElement *element = [elements objectAtIndex:0];
            TFHppleElement *child = [element.children objectAtIndex:0];
            NSString *idValue = [child content];

            NSString *idwithxml = [idValue stringByAppendingFormat:@".xml"];
            NSString *url = @"http://www.wccca.com/PITS/xml/fire_data_";
            NSString *finalurl = [url stringByAppendingString:idwithxml];

            xmlParser = [[XMLParser alloc] loadXMLByURL:finalurl];

            [callsTableView reloadData];
        }
    }

else {

    NSLog(@"Not Reachable");

    self.headerHeight = 0.0;

    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"No Internet Connection"
                                                      message:@"Please check your internet connection and pull down to refresh."
                                                     delegate:self
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];
    [message show];
    [message release];
}

[pull finishedLoading];
}

You have two problems: 您有两个问题:

  1. You shouldn't be using Reachability to determine whether there is an internet connection. 您不应该使用“可达性”来确定是否存在互联网连接。 Reachability is only useful to determine when an offline connection comes back online. 可达性仅在确定脱机连接何时重新联机时才有用。 Really. 真。 Making the networking calls is sometimes enough to power up the radios and connect so if you try to pre-check whether there is an internet connection the radios will remain off. 进行网络通话有时足以打开无线电并连接,因此,如果您尝试预先检查是否存在互联网连接,则无线电将保持关闭状态。

  2. The second problem is that no synchronous networking calls can be made on the main thread. 第二个问题是无法在主线程上进行同步网络调用。 This includes Reachability and [NSData dataWithContentsOfURL:xxx]. 这包括可达性和[NSData dataWithContentsOfURL:xxx]。 Make them on a secondary thread using Grand Central Dispatch, NSOperationQueues, NSURLConnections, or NSThreads. 使用Grand Central Dispatch,NSOperationQueues,NSURLConnections或NSThreads将它们置于辅助线程上。 Otherwise your application can lock up if the network isn't in good shape and the system watchdog timer will kill your app. 否则,如果网络状况不佳,您的应用程序可能会锁定,并且系统看门狗计时器将杀死您的应用程序。

I tested the following method using sendAsynchronousRequest:queue:completionHandler: as the method to download data from the URL. 我使用sendAsynchronousRequest:queue:completionHandler:作为从URL下载数据的方法,测试了以下方法。 It seems to work fine, I can get the else clause to fire, either by messing up the URL or making the timeout interval .1 seconds. 看来工作正常,我可以通过弄乱URL或将超时间隔设置为.1秒来启动else子句。

    NSURL *theURL = [NSURL URLWithString:@"http://www.wccca.com/PITS/"];

    NSURLRequest *theRequest = [NSURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5];
    [NSURLConnection sendAsynchronousRequest:theRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if (error == nil) {
            //do whatever with the data.  I converted it to a string for testing purposes.
            NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"%@",text);
        }else{
            NSLog(@"%@",error.localizedDescription);
            UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"Please check your internet connection and pull down to refresh." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [message show];
        }
    }];

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

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