Skip to content

Commit

Permalink
[iOS] Support RN 0.46+, realm#1121
Browse files Browse the repository at this point in the history
  • Loading branch information
ovr authored Jul 6, 2017
1 parent 80253cd commit fd1d175
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions react-native/ios/RealmReact/RealmReact.mm
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ - (JSContext *)context;
// the part of the RCTCxxBridge private class we care about
@interface RCTBridge (RCTCxxBridge)
- (JSGlobalContextRef)jsContextRef;
// RN >= 0.46
- (void)executeBlockOnJavaScriptThread:(dispatch_block_t)block;
// RN < 0.46
- (void)ensureOnJavaScriptThread:(dispatch_block_t)block;
@end

extern "C" JSGlobalContextRef RealmReactGetJSGlobalContextForExecutor(id executor, bool create) {
Expand Down Expand Up @@ -305,17 +308,26 @@ - (void)setBridge:(RCTBridge *)bridge {

__weak __typeof__(self) weakSelf = self;
__weak __typeof__(bridge) weakBridge = bridge;
[bridge executeBlockOnJavaScriptThread:^{

dispatch_block_t bridgeInitializer = ^{
__typeof__(self) self = weakSelf;
__typeof__(bridge) bridge = weakBridge;
if (!self || !bridge) {
return;
}

_initializeOnJSThread(^{
return [bridge jsContextRef];
});
}];
};

Class RCTJavaScriptContext = NSClassFromString(@"RCTCxxBridge");
if ([RCTJavaScriptContext instancesRespondToSelector:@selector(ensureOnJavaScriptThread:)]) {
[bridge ensureOnJavaScriptThread:bridgeInitializer];
} else {
[bridge executeBlockOnJavaScriptThread:bridgeInitializer];
}

return;
} else { // React Native 0.44 and older
id<RCTJavaScriptExecutor> executor = [bridge valueForKey:@"javaScriptExecutor"];
Expand Down

0 comments on commit fd1d175

Please sign in to comment.