iOS 9 - 网络可达性变化导致 View 卡住 - 不是崩溃

标签 ios objective-c ios9 xcode7 reachability

升级到 iOS 9 后,我的应用出现了一个奇怪的问题。

问题:每当网络可达性发生变化时,应用程序的 View 就会卡住。它不响应任何触摸事件。它也没有坠毁。我检查了日志。唯一的变化是网络可达性。

更多信息:

1) 如果您退出该应用程序并通过互联网再次打开,它运行正常。

2) 如果我在离线状态下打开应用程序, View 会卡住。这不会发生在 iOS 8.4 中。

我正在使用 Reachability by Tony million用于检查在线/离线状态的库。

有没有人在升级到 iOS 9 后遇到同样的问题?

引用代码:

self.appIsOnline = YES;
_reachability = [Reachability reachabilityWithHostName:@"www.google.com"];

__weak __typeof(self)weakSelf = self;
[_reachability setReachableBlock: ^(Reachability * reachability) {

    NSLog(@"Online");

    if (!weakSelf.appIsOnline)
    {
        weakSelf.appIsOnline = YES;
        weakSelf.willShowAlertView = YES;
        dispatch_async(dispatch_get_main_queue(), ^{
            CustomAlertView *alertView = [[CustomAlertView alloc] initWithTitle:@"Warning" andMessage:@"The internet connection appears to be online. The app will switch to online mode now" delegate:weakSelf withButtons:@[@"OK"]];
            alertView.shouldTapOutsideToClose = YES;
            [alertView showInView:MAIN_WINDOW];
        });
    }
}];
[_reachability setUnreachableBlock:^(Reachability * reachability) {

    NSLog(@"Offline");

    if (weakSelf.appIsOnline)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:OFFLINE_STATE_NOTIFICATION object:nil];
        weakSelf.appIsOnline = NO;
        dispatch_async(dispatch_get_main_queue(), ^{
            [[AppUtilities sharedUtilities] displayAlertViewWithTitle:@"Warning" andMessage:@"The internet connection appears to be offline. The app will switch to offline mode now"];
        });
    }
}];

[_reachability startNotifier];

看起来我的主 UI 线程被阻止了,触发这个的是关闭 wi-fi。我的线程跟踪如下。

Thread 1Queue : com.apple.main-thread (serial)

#0  0x38739130 in mach_msg_trap ()
#1  0x38738f30 in mach_msg ()
#2  0x265624ec in __CFRunLoopServiceMachPort ()
#3  0x26560872 in __CFRunLoopRun ()
#4  0x264b31e8 in CFRunLoopRunSpecific ()
#5  0x264b2fdc in CFRunLoopRunInMode ()
#6  0x2f757af8 in GSEventRunModal ()
#7  0x2a71818c in UIApplicationMain ()
#8  0x00117f20 in main

Thread 3Queue : com.apple.libdispatch-manager (serial)

#0  0x3874e3c0 in kevent_qos ()
#1  0x0067d5f6 in _dispatch_mgr_invoke ()
#2  0x0066ea76 in _dispatch_mgr_thread ()

com.apple.NSURLConnectionLoader (11)

#0  0x38739130 in mach_msg_trap ()
#1  0x38738f30 in mach_msg ()
#2  0x265624ec in __CFRunLoopServiceMachPort ()
#3  0x26560872 in __CFRunLoopRun ()
#4  0x264b31e8 in CFRunLoopRunSpecific ()
#5  0x264b2fdc in CFRunLoopRunInMode ()
#6  0x25e240ae in +[NSURLConnection(Loader) _resourceLoadLoop:] ()
#7  0x273747fc in __NSThread__start__ ()
#8  0x387ebc92 in _pthread_body ()
#9  0x387ebc06 in _pthread_start ()
#10 0x387e9a24 in thread_start ()

AFNetworking (12)#0 0x38739130 in mach_msg_trap ()

#1  0x38738f30 in mach_msg ()
#2  0x265624ec in __CFRunLoopServiceMachPort ()
#3  0x26560872 in __CFRunLoopRun ()
#4  0x264b31e8 in CFRunLoopRunSpecific ()
#5  0x264b2fdc in CFRunLoopRunInMode ()
#6  0x272a3d7c in -[NSRunLoop(NSRunLoop) runMode:beforeDate:] ()
#7  0x272f28ec in -[NSRunLoop(NSRunLoop) run] ()
#8  0x00122a2e in +[AFURLConnectionOperation networkRequestThreadEntryPoint:] at /Pods/AFNetworking/AFNetworking/AFURLConnectionOperation.m:168
#9  0x273747fc in __NSThread__start__ ()
#10 0x387ebc92 in _pthread_body ()
#11 0x387ebc06 in _pthread_start ()
#12 0x387e9a24 in thread_start ()

com.apple.CFSocket.private (13)#0   0x3874cfb4 in __select ()

#1  0x26567990 in __CFSocketManager ()
#2  0x387ebc92 in _pthread_body ()
#3  0x387ebc06 in _pthread_start ()
#4  0x387e9a24 in thread_start ()

最佳答案

我终于找到了真正的问题。它不具有可达性等级。这是由于我的 CustomAlertView。每当可达性发生变化时,我的应用程序都会弹出一条警告,说明离线/在线。它像这样直接在我的主窗口上显示警报 View

[alertView showInView:MAIN_WINDOW];

在哪里

#define MAIN_WINDOW         [UIApplication sharedApplication].keyWindow

在 iOS 9 中这是不允许的。当它尝试显示时,UI 线程暂停。我通过这样做解决了它

[alertView showInView:MAIN_WINDOW.rootViewController.view]; 

关于iOS 9 - 网络可达性变化导致 View 卡住 - 不是崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32862108/

相关文章:

ios - 如何在弹出窗口打开时打开 View Controller - swift 3

c++ - defaultUserNotificationCenter 在 C++ 中导致 'unrecognized selector' 错误

objective-c - 从另一个 Controller 和 Nib 加载 View

ios - UIView动画选项没有效果

ios - 此应用程序正在从后台线程修改自动布局引擎 - ios 9

ios - 当文本字段为空时如何启用按钮

ios - 更改导航栏中的标签文本

ios - 如何确定从外部didSelectRowAtIndexPath(Objective-C)在UITableView中选择了哪些行

ios - FacebookSDK iOS v3.24.0-beta1

ios - 错误 : Unable to resolve build file: XCBCore. BuildFile (namedReferencesCannotBeResolved)(在目标 'CentralCasting' 中)