主线程阻塞导致应用程序在iOS 8中崩溃

Kun*_*ani 1 iphone reachability ios ios8

我的应用程序在启动时崩溃,因为"未能及时更新场景"

这是因为可达性不能按时返回这是堆栈跟踪

com.tjango.Plus3 failed to scene-update in time

Elapsed total CPU time (seconds): 2.590 (user 2.590, system 0.000), 12% CPU 
Elapsed application CPU time (seconds): 0.302, 1% CPU


Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0:
_semaphore_wait_trap + 8
__dispatch_semaphore_wait_slow + 252
_xpc_connection_send_message_with_reply_sync + 184
___SCNetworkReachabilityServer_targetStatus + 192
___SCNetworkReachabilityGetFlags + 440
_SCNetworkReachabilityGetFlags + 232
-[Reachability currentReachabilityStatus] Reachability.m:295

这只发生在iOS 8上.

它被卡住的功能是Apple的Reachability库:我无法弄清楚为什么这个功能会阻止它.

- (NetworkStatus)currentReachabilityStatus

{

    NSAssert(_reachabilityRef != NULL, @"currentNetworkStatus called with NULL SCNetworkReachabilityRef");

    NetworkStatus returnValue = NotReachable;

    SCNetworkReachabilityFlags flags;



    if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags))

    {

        if (_alwaysReturnLocalWiFiStatus)

        {

            returnValue = [self localWiFiStatusForFlags:flags];

        }

        else

        {

            returnValue = [self networkStatusForFlags:flags];

        }

    }



    return returnValue;

}

Eri*_*icS 7

可达性可能是iOS上最不正确使用的库.这些规则有助于:

  1. 不应在主线程上调用可达性.在辅助线程上调用它并让该线程通知主线程网络更改.
  2. 在尝试连接之前不要检查网络状态 - 只需尝试连接并根据需要处理错误.有时网络将脱机以节省电量,直到有人尝试连接.
  3. 可达性主要用于检测网络脱机后何时恢复在线状态.您可以在此时重试失败的连接.

请参阅我的评论:iOS Reachability无法捕捉连接到WiFi但未登录的情况