码迷,mamicode.com
首页 > 编程语言 > 详细

RN 47 中的 JS 线程及 RunLoop

时间:2017-08-20 15:54:57      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:sop   art   dna   etc   ext   opd   ninm   ati   mon   

RCBridge 初始化时声明了一个 CADisplayLink

_jsDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(_jsThreadUpdate:)];

_jsThreadUpdate 函数中,处理界面更新。这个 CADisplayLink 随后被加到 JS 线程对应的 RunLoop 中。

- (void)addToRunLoop:(NSRunLoop *)runLoop
{
  _runLoop = runLoop;
  [_jsDisplayLink addToRunLoop:runLoop forMode:NSRunLoopCommonModes];
}

RCTCxxBridge 声明了一个 thread

  _jsThread = [[NSThread alloc] initWithTarget:self
                                      selector:@selector(runJSRunLoop)
                                        object:nil];
  _jsThread.name = RCTJSThreadName;
  _jsThread.qualityOfService = NSOperationQualityOfServiceUserInteractive;
  [_jsThread start];

并为这个 thread 声明了要给 RunLoop

// copy thread name to pthread name
pthread_setname_np([NSThread currentThread].name.UTF8String);

// Set up a dummy runloop source to avoid spinning
CFRunLoopSourceContext noSpinCtx = {0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
CFRunLoopSourceRef noSpinSource = CFRunLoopSourceCreate(NULL, 0, &noSpinCtx);
CFRunLoopAddSource(CFRunLoopGetCurrent(), noSpinSource, kCFRunLoopDefaultMode);
CFRelease(noSpinSource);

RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");

// run the run loop
while (kCFRunLoopRunStopped != CFRunLoopRunInMode(kCFRunLoopDefaultMode, ((NSDate *)[NSDate distantFuture]).timeIntervalSinceReferenceDate, NO)) {
  RCTAssert(NO, @"not reached assertion"); // runloop spun. that's bad.
}

RunLoop 的调用方式之 performSelector

技术分享?

如下,原因是因为有如下调用:

  if ([NSThread currentThread] == _jsThread) {
    [self _tryAndHandleError:block];
  } else {
    [self performSelector:@selector(_tryAndHandleError:)
          onThread:_jsThread
          withObject:block
          waitUntilDone:NO];
  }

在 RunLoop 上执行一函数

  CFRunLoopPerformBlock(m_cfRunLoop, kCFRunLoopCommonModes, ^{ func(); });

Enqueues a block object on a given runloop to be executed as the runloop cycles in specified modes.

CADisplayLink 加到指定的 RunLoop 中,然后相应

技术分享?

_jsDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(_jsThreadUpdate:)];
[_jsDisplayLink addToRunLoop:runLoop forMode:NSRunLoopCommonModes];

RN 47 中的 JS 线程及 RunLoop

标签:sop   art   dna   etc   ext   opd   ninm   ati   mon   

原文地址:http://www.cnblogs.com/huahuahu/p/RN-47-zhong-de-JS-xian-cheng-ji-RunLoop.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!