首页 > 解决方案 > 我可以实例化两个 RCTBridge 实例吗?

问题描述

出于某种原因,当我尝试实例化一个额外的RCTBridge实例时,只有一个被启动(首先创建的那个)。这是我的 AppDelegate 中的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // Setup sync worker
  self.syncThreadBridge = [[RCTBridge alloc] initWithDelegate:[SyncWorkerBridgeDelegate new] launchOptions:nil];
  // React Native root view
  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:@"Main_app"
                                            initialProperties:nil];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  // Return
  return YES;
}

使用此代码,仅启动同步线程桥,而不启动应用程序的主桥(但同步线程桥成功初始化并向 Reactotron 发送消息)。

如果我将同步线程桥初始化移到主应用桥初始化下方,则主应用被初始化,呈现给用户,主应用向 Reactotron 发送消息。但是,同步线程桥不会启动,也不会连接到 Reactotron。

我的方法基于来自https://github.com/joltup/react-native-threads的代码——那里的示例代码似乎不适用于我使用的 React Native 0.63。

那么创建多个 JS 运行时的选项被删除了吗?

标签: iosobjective-creact-native

解决方案


实际上,我只是在一个全新的 React Native 0.63.4 应用程序上尝试过这个,它可以工作:https ://github.com/backmeupplz/react-native-ios-threading-example


推荐阅读