升级Xcode 14.2后React Native iOS应用UI冻结无响应问题
问题:Xcode 14.2升级后React Native iOS应用启动冻结无响应
我开发了一款支持多语言的React Native应用,可在Android和iOS平台运行。将Xcode升级至14.2后,iOS端应用启动后出现屏幕冻结、无响应的情况。
Xcode日志信息
Thread Performance Checker: Thread running at QOS_CLASS_USER_INTERACTIVE waiting on a lower QoS thread running at QOS_CLASS_DEFAULT. Investigate ways to avoid priority inversions PID: 97834, TID: 527115 Backtrace ================================================================= 3 planetTube 0x0000000100c22b1c -[_RCTSRRunLoopThread runLoop] + 44 4 planetTube 0x0000000100c2284c __49+[NSRunLoop(RCTSRWebSocket) RCTSR_networkRunLoop]_block_invoke + 116 5 libdispatch.dylib 0x0000000103691d5c _dispatch_client_callout + 16 6 libdispatch.dylib 0x0000000103693800 _dispatch_once_callout + 80 7 planetTube 0x0000000100c227b0 +[NSRunLoop(RCTSRWebSocket) RCTSR_networkRunLoop] + 84 8 planetTube 0x0000000100c1c7ec -[RCTSRWebSocket _connect] + 68 9 planetTube 0x0000000100c1b590 -[RCTSRWebSocket open] + 300 10 planetTube 0x0000000100bfd9d0 -[RCTReconnectingWebSocket start] + 148 11 planetTube 0x0000000100bed3bc -[RCTPackagerConnection init] + 404 12 planetTube 0x0000000100bed208 __49+[RCTPackagerConnection sharedPackagerConnection]_block_invoke + 36 13 libdispatch.dylib 0x0000000103691d5c _dispatch_client_callout + 16 14 libdispatch.dylib 0x0000000103693800 _dispatch_once_callout + 80 15 planetTube 0x0000000100bed1bc +[RCTPackagerConnection sharedPackagerConnection] + 84 16 planetTube 0x0000000100c87964 -[RCTDevSettings initialize] + 148 17 planetTube 0x0000000100bd69d0 -[RCTModuleData _initializeModule] + 96 18 planetTube 0x0000000100bd6330 -[RCTModuleData setUpInstanceAndBridge:] + 2120 19 planetTube 0x0000000100bd7de8 -[RCTModuleData instance] + 1168 20 planetTube 0x0000000100b76f8c -[RCTCxxBridge moduleForName:lazilyLoadIfNecessary:] + 680 21 planetTube 0x0000000100be0ba0 -[RCTModuleRegistry moduleForName:lazilyLoadIfNecessary:] + 128 22 planetTube 0x0000000100be0b14 -[RCTModuleRegistry moduleForName:] + 48 23 planetTube 0x0000000100c9bd64 -[RCTPerfMonitor devMenuItem] + 84 24 planetTube 0x0000000100c9bc38 -[RCTPerfMonitor initialize] + 88 25 planetTube 0x0000000100bd69d0 -[RCTModuleData _initializeModule] + 96 26 planetTube 0x0000000100bd6330 -[RCTModuleData setUpInstanceAndBridge:] + 2120 27 planetTube 0x0000000100bd8018 __25-[RCTModuleData instance]_block_invoke + 44 28 planetTube 0x0000000100c409dc RCTUnsafeExecuteOnMainQueueSync + 52 29 planetTube 0x0000000100bd7c88 -[RCTModuleData instance] + 816 30 planetTube 0x0000000100b7b7f8 __49-[RCTCxxBridge _prepareModulesWithDispatchGroup:]_block_invoke + 156 31 libdispatch.dylib 0x0000000103690594 _dispatch_call_block_and_release + 24 32 libdispatch.dylib 0x0000000103691d5c _dispatch_client_callout + 16 33 libdispatch.dylib 0x00000001036a2978 _dispatch_main_queue_drain + 1684 34 libdispatch.dylib 0x00000001036a22d4 _dispatch_main_queue_callback_4CF + 40 35 CoreFoundation 0x0000000180372ca4 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12 36 CoreFoundation 0x000000018036d360 __CFRunLoopRun + 1956 37 CoreFoundation 0x000000018036c7a4 CFRunLoopRunSpecific + 584 38 GraphicsServices 0x0000000188ff7c98 GSEventRunModal + 160 39 UIKitCore 0x000000010ee2237c -[UIApplication _run] + 868 40 UIKitCore 0x000000010ee26374 UIApplicationMain + 124 41 planetTube 0x00000001008a214c main + 100 2023-03-19 14:05:21.846279+0530 planetTube[97834:527225] [connection] nw_socket_handle_socket_event [C5.1.1:1] Socket SO_ERROR [61: Connection refused] 2023-03-19 14:05:21.847297+0530 planetTube[97834:527225] [connection] nw_socket_handle_socket_event [C5.1.2:1] Socket SO_ERROR [61: Connection refused] 2023-03-19 14:05:21.847706+0530 planetTube[97834:527231] [connection] nw_connection_get_connected_socket_block_invoke [C5] Client called nw_connection_get_connected_socket on unconnected nw_connection 2023-03-19 14:05:21.847810+0530 planetTube[97834:527231] TCP Conn 0x6000007f4000 Failed : error 0:61 [61] 2023-03-19 14:05:22.307289+0530 planetTube[97834:527223] [native] Manifest does not exist - creating a new one. (null) 2023-03-19 14:05:22.386170+0530 planetTube[97834:527115] [native] [GESTURE HANDLER] Initialize gesture handler for view <RCTRootContentView: 0x12c50af90; reactTag: 1; frame = (0 0; 393 852); gestureRecognizers = <NSArray: 0x6000038e3bd0>; layer = <CALayer: 0x6000036c0500>>
问题分析
从日志能定位两个核心问题:
- 线程优先级倒置:负责用户交互的主线程(QOS_CLASS_USER_INTERACTIVE)正在等待一个默认优先级(QOS_CLASS_DEFAULT)的网络线程,直接导致主线程阻塞,应用失去响应。
- WebSocket连接失败:日志显示Socket连接被拒绝(错误61),这是Debug模式下应用尝试连接Metro打包服务失败导致的,进一步加重了主线程的阻塞情况。
解决方案
- 区分环境初始化开发模块:在非Debug环境下,禁用RCTPackagerConnection、RCTPerfMonitor等开发相关模块的初始化,避免主线程等待失败的WebSocket连接。可在AppDelegate中添加环境判断逻辑,仅在Debug模式下初始化这些模块。
- 确保Debug环境Metro服务正常:启动应用前先运行
npx react-native start,保证打包服务可用;同时检查iOS网络配置,在Info.plist中添加NSAppTransportSecurity例外,允许HTTP连接到本地Metro服务。 - 修复线程优先级:修改React Native源码中
_RCTSRRunLoopThread的线程优先级,将其QoS等级设置为QOS_CLASS_USER_INTERACTIVE或QOS_CLASS_UTILITY,避免主线程等待低优先级线程。 - 升级React Native版本:旧版本React Native可能存在与Xcode 14.2的兼容性问题,升级到最新稳定版可修复这类线程阻塞和初始化异常。
- 清理各类缓存:执行以下命令排除缓存导致的问题:
同时通过Xcode快捷键# 清理iOS Pod缓存 pod deintegrate && pod install # 清理React Native缓存 npx react-native start --reset-cacheCmd + Shift + K清理Xcode缓存。
内容的提问来源于stack exchange,提问作者Chaitanya Parashar
相关产品推荐
相关产品推荐

