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 committed Jul 6, 2017
1 parent 80253cd commit 7a9d469
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions react-native/ios/RealmReact/RealmReact.mm
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ - (JSContext *)context;
// the part of the RCTCxxBridge private class we care about
@interface RCTBridge (RCTCxxBridge)
- (JSGlobalContextRef)jsContextRef;
// RN >= 0.46
- (void)ensureOnJavaScriptThread:(dispatch_block_t)block;
// RN < 0.46
- (void)executeBlockOnJavaScriptThread:(dispatch_block_t)block;
@end

Expand Down Expand Up @@ -305,7 +308,8 @@ - (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) {
Expand All @@ -315,7 +319,15 @@ - (void)setBridge:(RCTBridge *)bridge {
_initializeOnJSThread(^{
return [bridge jsContextRef];
});
}];
};

Class RCTCxxBridgeClass = NSClassFromString(@"RCTCxxBridge");
if ([RCTCxxBridgeClass 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 7a9d469

Please sign in to comment.