Where should I perform a Reachability check?

霸气de小男生 提交于 2019-12-09 19:22:51

问题


I want to check for a valid network connection. I followed Apple's Reachability example and put my check in applicationDidFinishLaunching

#pragma mark -
#pragma mark Application lifecycle

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

        if(getenv("NSZombieEnabled") || getenv("NSAutoreleaseFreedObjectCheckEnabled")) 
        {
            NSLog(@"NSZombieEnabled/NSAutoreleaseFreedObjectCheckEnabled enabled!");
        }

        // Override point for customization after application launch.
        [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];

        //Check for connectivity
        internetReach = [[Reachability reachabilityForInternetConnection] retain];
        [internetReach startNotifer];
        [self updateInterfaceWithReachability: internetReach];

        [window addSubview:navigationController.view];
        [window makeKeyAndVisible];

        return YES;
    }

However, my app will crash sometimes with the error Application Failed to Launch in Time

I have posted my crash as an SO question here: Application Failed to Launch in Time

I'm not sure where I should perform the reachability check?


回答1:


In -applicationDidBecomeActive you may call a method in the background that uses the reachability code with -performSelectorInBackground:withObject:.




回答2:


A Reachability check could take a significant amount of time (30 seconds or more) depending on network conditions. But if your app's UI does not respond for some number of seconds (much less than 30), the OS assumes that it is dead and kills it.

If you do your Reachability check in a background thread, not the UI thread, then your UI will stay responsive, and neither the OS nor the user will assume that your app has locked up or crashed.



来源:https://stackoverflow.com/questions/3953717/where-should-i-perform-a-reachability-check

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!