I’ve inherited a React Native app and there’s a bridge class which is being instantiated numerous times (on iOS)
Here’s a bit of code:
@interface RCT_EXTERN_MODULE(TheNativeBridge, RCTEventEmitter)
some RCT_EXTERN_METHODs
@end
@interface TheNativeBridge()
some ObjC method declarations
@end
@implementation TheNativeBridge
- (NSArray<NSString *> *) supportedEvents {<snip>}
- (id) init {
self = [super init];
return self;
}
<snip>
@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
<snip>
When the app runs, the TheNativeBridge:init() method is getting called seven times by RCTxxBridge.mm _registerModulesForClasses() (which in turn being invoked from didFinishLaunchingWithOptions).
What’s going on here? Is something set up incorrectly and so RCTxxBridge.mm is instantiating it too many times, or is TheNativeBridge class supposed to be implemented as a singleton? (I don’t see that being mentioned as something necessary to do in the documentation)