We have some native background code that runs periodically and trying to lower the energy impact of the app when in the background (iOS).
Currently, I'm invalidating RCTBridge since that seems to unload the entirety of react-native rendering and processing.
- (void) applicationDidEnterBackground:(UIApplication *)application
{
[self.bridge invalidate];
self.bridge = nil;
}
and then when the app comes back into foreground I'm re-initializing RCTBridge and RCTRootView.
- (void) applicationWillEnterForeground:(UIApplication *)application
{
if(self.bridge == nil) {
self.bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:nil];
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:self.bridge
moduleName:@"MyApp"
initialProperties:nil];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
}
}
The tradeoff of this method is when the app enters the foreground each time it feels like a cold boot of the app.
Is there a better way (possible on the react-native side or objective-c side) to pause CPU usage caused by react-native when in background?