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
CADisplayLink
Add 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