JS thread and Runloop in RN 47

Source: Internet
Author: User

Rcbridge is initialized with a declaration of a CADisplayLink
_jsDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(_jsThreadUpdate:)];

In the _jsThreadUpdate function, the interface updates are processed. This CADisplayLink is then added to the runloop corresponding to the JS thread.

- (void)addToRunLoop:(NSRunLoop *)runLoop{  _runLoop = runLoop;  [_jsDisplayLink addToRunLoop:runLoop forMode:NSRunLoopCommonModes];}
Rctcxxbridge declares a thread
  _jsThread = [[NSThread alloc] initWithTarget:self                                      selector:@selector(runJSRunLoop)                                        object:nil];  _jsThread.name = RCTJSThreadName;  _jsThread.qualityOfService = NSOperationQualityOfServiceUserInteractive;  [_jsThread start];

and declared for this thread to be given to Runloop.

// copy thread name to pthread namepthread_setname_np([NSThread currentThread].name.UTF8String);// Set up a dummy runloop source to avoid spinningCFRunLoopSourceContext 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 loopwhile (kCFRunLoopRunStopped != CFRunLoopRunInMode(kCFRunLoopDefaultMode, ((NSDate *)[NSDate distantFuture]).timeIntervalSinceReferenceDate, NO)) {  RCTAssert(NO, @"not reached assertion"); // runloop spun. that's bad.}
The method of Runloop invocation performSelector

?

As follows, the reason is because of the following call:

  if ([NSThread currentThread] == _jsThread) {    [self _tryAndHandleError:block];  } else {    [self performSelector:@selector(_tryAndHandleError:)          onThread:_jsThread          withObject:block          waitUntilDone:NO];  }
Perform a function on the Runloop
  CFRunLoopPerformBlock(m_cfRunLoop, kCFRunLoopCommonModes, ^{ func(); });

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

Put CADisplayLinkAdd to the specified Runloop, and then the corresponding

?

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

JS thread and Runloop in RN 47

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.