Skip to content

Commit

Permalink
fix(ios): fix random crash on startup with Hermes (#1051)
Browse files Browse the repository at this point in the history
Hermes prefers executing code on its dedicated thread, and we haven't
been retrieving and using the Runtime instance properly.
  • Loading branch information
tido64 authored Aug 17, 2022
1 parent 1142bb6 commit de978fc
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions ios/ReactTestApp/AppRegistryModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

@interface RCTCxxBridge : RCTBridge
@property (nonatomic, readonly) void *runtime;
- (void)invokeAsync:(std::function<void()> &&)func;
@end

@implementation RTAAppRegistryModule
Expand Down Expand Up @@ -38,28 +39,33 @@ - (instancetype)init
- (void)javascriptDidLoadNotification:(NSNotification *)note
{
if (![self.bridge isKindOfClass:[RCTCxxBridge class]] ||
![self.bridge respondsToSelector:@selector(runtime)]) {
![self.bridge respondsToSelector:@selector(runtime)] ||
![self.bridge respondsToSelector:@selector(invokeAsync:)]) {
return;
}

auto runtimePtr = ((RCTCxxBridge *)self.bridge).runtime;
if (runtimePtr == nullptr) {
return;
}

auto appKeys = ReactTestApp::GetAppKeys(*static_cast<Runtime *>(runtimePtr));
if (appKeys.empty()) {
return;
}

NSMutableArray *array = [NSMutableArray arrayWithCapacity:appKeys.size()];
for (const auto &appKey : appKeys) {
[array addObject:[NSString stringWithUTF8String:appKey.c_str()]];
}

[NSNotificationCenter.defaultCenter postNotificationName:ReactTestAppDidRegisterAppsNotification
object:self
userInfo:@{@"appKeys": [array copy]}];
auto batchedBridge = (RCTCxxBridge *)self.bridge;
[batchedBridge invokeAsync:[batchedBridge] {
auto runtime = static_cast<Runtime *>(batchedBridge.runtime);
if (runtime == nullptr) {
return;
}

auto appKeys = ReactTestApp::GetAppKeys(*runtime);
if (appKeys.empty()) {
return;
}

NSMutableArray *array = [NSMutableArray arrayWithCapacity:appKeys.size()];
for (const auto &appKey : appKeys) {
[array addObject:[NSString stringWithUTF8String:appKey.c_str()]];
}

[NSNotificationCenter.defaultCenter
postNotificationName:ReactTestAppDidRegisterAppsNotification
object:nil
userInfo:@{@"appKeys": [array copy]}];
}];
}

@end

0 comments on commit de978fc

Please sign in to comment.