iOS14.x生产环境Bugsnag主线程调用随机异常求助
Hey there, let's break down how to fix this random main-thread crash tied to Firebase push notifications on iOS 14.0/14.0.1. This issue stems from older versions of react-native-firebase not properly handling thread safety for iOS 14's notification system—some Firebase callbacks fire on background threads but require main-thread execution for React Native operations, triggering that folly-related exception.
1. Upgrade Dependencies (Long-Term, Most Reliable Fix)
Version 5.5.5 of react-native-firebase is outdated and doesn't account for iOS 14's threading changes. Upgrading to a supported version will resolve the root cause:
- Uninstall the old package:
npm uninstall react-native-firebase - Install a compatible newer version (v6.x+ works with React Native 0.61.4):
npm install @react-native-firebase/app @react-native-firebase/messaging - Reinstall iOS pods:
cd ios && pod install && cd .. - Update your
GoogleService-Info.plistto match the latest Firebase SDK requirements - Adjust your notification setup code to align with the v6.x API (most changes are minor, like importing from
@react-native-firebase/messaginginstead of the oldreact-native-firebasepath)
2. Manual Thread Safety Fix (Temporary Workaround)
If upgrading isn't feasible right now, you can force Firebase notification callbacks to run on the main thread. Here's how to implement this:
iOS Native Code Adjustment
Open your AppDelegate.m and wrap Firebase notification handling in a main-thread dispatch:
// AppDelegate.m - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // Force Firebase processing to run on main thread dispatch_async(dispatch_get_main_queue(), ^{ [[RNFirebaseMessaging instance] didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler]; }); } // Handle foreground notifications the same way - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { dispatch_async(dispatch_get_main_queue(), ^{ [[RNFirebaseMessaging instance] willPresentNotification:notification withCompletionHandler:completionHandler]; }); }
React Native JS Adjustment
In your JS code, ensure any UI or state updates triggered by notifications are wrapped to run on the main thread:
import { InteractionManager, Alert } from 'react-native'; import messaging from 'react-native-firebase/messaging'; // For v5.x messaging().onNotification((remoteMessage) => { // Use InteractionManager to guarantee main-thread execution for UI updates InteractionManager.runAfterInteractions(() => { Alert.alert('New Notification', remoteMessage.notification.body); // Add your state update or navigation logic here }); });
3. Additional Debugging & Mitigation
- Capture Thread Context in Bugsnag: Modify your crash reporting to log the current thread name when the exception occurs. This will help you pinpoint exactly which Firebase callback is firing off-thread.
- Disable Firebase App Delegate Proxy: Add
FirebaseAppDelegateProxyEnabledto yourInfo.plistwith a value ofNO. This forces manual handling of all notification logic, avoiding automatic thread mismanagement from the proxy. Just make sure you implement all required notification delegate methods in yourAppDelegate.
内容的提问来源于stack exchange,提问作者Sonam Maniar

