首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有可达性专家?

有没有可达性专家?
EN

Stack Overflow用户
提问于 2011-12-03 05:36:30
回答 1查看 1K关注 0票数 0

我的应用程序一直运行得很好,直到我将Reachability .h和.m文件合并到它中,以检查互联网连接。虽然在大多数情况下,一切仍然运行得很好(所有网页都加载正确,当我故意关闭我的Airport进行测试时,Reachability会优雅地捕捉到它)--我现在确实在奇怪的时候会莫名其妙地崩溃,并出现可怕的"Exc-Bad-Access“错误……我运行了Instruments并找到了僵尸-参见屏幕-抓取:

查看"RefCt“列,您可以看到它达到了15 -我看到它超过了20!在"Responsible Caller“列(最右边)中,我看到了各种我不认识的方法--而且谁的调用肯定不是我发起的--我假设它们是在运行时由系统内部调用的?

无论哪种方式,我都非常仔细地遵循了苹果的可达性代码和说明,以及来自排名非常高的堆栈溢出线程的提示:How to check for an active Internet connection on iOS or OSX?

但我还是会遇到一些莫名其妙的崩溃。

有人能给点建议吗?

代码如下:

代码语言:javascript
复制
#import "Reachability.h"


-(void) viewWillAppear:(BOOL)animated
{
   // check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];

// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[hostReachable startNotifier];

// now patiently wait for the notification
}



-(void) viewDidLoad {

    url = [NSURL URLWithString: @"http://www.google.com"];
    NSURLRequest *req = [NSURLRequest requestWithURL: url];
    [webPageView loadRequest:req];
    [super viewDidLoad];
}



-(void) checkNetworkStatus:(NSNotification *)notice
{
        // called after network status changes

    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];

    switch (internetStatus) 
    {
        case NotReachable:
        {
            [self displayMessageWithTitle:@"ERROR"
                               andMessage:@"Unable To Connect to Internet"
                              andOKButton:@"OK"];
            [self dismissModalViewControllerAnimated:YES];
            break;

        }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI.");

            break;

        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN.");

            break;

        }
    }

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)
    {
        case NotReachable:
        {
            // NSLog(@"A gateway to the host server is down.");

            [self displayMessageWithTitle:@"ERROR"
                               andMessage:@"Host Not Reachable"
                              andOKButton:@"OK"];
            [self dismissModalViewControllerAnimated:YES];
            break;

        }

        case ReachableViaWiFi:
        {
            NSLog(@"A gateway to the host server is working via WIFI.");

            break;

        }

        case ReachableViaWWAN:
        {
            NSLog(@"A gateway to the host server is working via WWAN.");

            break;

        }
    }
}



- (void)webViewDidStartLoad:(UIWebView *)webView {
    [activityIndicator startAnimating];
}


- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [activityIndicator stopAnimating];
}


// Since this ViewController is presented Modally, this method removes it 
// via a button click:
-(IBAction) dismissWebTixView:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}




-(void) viewWillDisappear:(BOOL)animated {
    NSLog(@"In webForTix's 'viewWillDisappear' method....");
    [[NSNotificationCenter defaultCenter] removeObserver:self
name:kReachabilityChangedNotification
                                              object:nil];
}


// Was told it might be good to put "removeObserver" here and not in viewWillDisappear
// well neither worked - the App still crashes....
- (void)dealloc
{
    //NSLog(@"In webForTix's dealloc method....");
//    [[NSNotificationCenter defaultCenter] removeObserver:self
//                                                        name:kReachabilityChangedNotification
//                                                  object:nil];
NSLog(@"In webForTix's dealloc method - removedObserver...");
[super dealloc];

}

EN

回答 1

Stack Overflow用户

发布于 2011-12-03 10:03:08

您必须小心地平衡对addObserver和removeObserver的调用。

如果在vieWillAppear中为通知添加观察者,则需要在viewWillDisappear中将其删除。

如果您添加了两次观察者,您将被调用两次以获取相同的通知,并且必须调用removeObserver两次才能删除这两个通知。

我没有看到任何明显的内存管理问题。看起来你发布的所有代码都在创建自动释放的对象,这应该没问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8362996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档