浏览代码

In iOS 9 / El Cap, NSTimer enforces non-nullability of target/action.

Instead of using a timer that will never fire, just cook up a no-op
runloop source in order to prevent the _SRRunLoopThread's runloop from
spinning while we wait for the stream's runloop source to be added.
James Howard 10 年之前
父节点
当前提交
9af1b11c1d
共有 1 个文件被更改,包括 16 次插入2 次删除
  1. 16 2
      SocketRocket/SRWebSocket.m

+ 16 - 2
SocketRocket/SRWebSocket.m

@@ -1749,8 +1749,22 @@ static NSRunLoop *networkRunLoop = nil;
         _runLoop = [NSRunLoop currentRunLoop];
         dispatch_group_leave(_waitGroup);
         
-        NSTimer *timer = [[NSTimer alloc] initWithFireDate:[NSDate distantFuture] interval:0.0 target:nil selector:nil userInfo:nil repeats:NO];
-        [_runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
+        // Add an empty run loop source to prevent runloop from spinning.
+        CFRunLoopSourceContext sourceCtx = {
+            .version = 0,
+            .info = NULL,
+            .retain = NULL,
+            .release = NULL,
+            .copyDescription = NULL,
+            .equal = NULL,
+            .hash = NULL,
+            .schedule = NULL,
+            .cancel = NULL,
+            .perform = NULL
+        };
+        CFRunLoopSourceRef source = CFRunLoopSourceCreate(NULL, 0, &sourceCtx);
+        CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
+        CFRelease(source);
         
         while ([_runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) {