From 45fa563d3a1dc49610f4b955b44adf9adcadefe9 Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Thu, 20 Mar 2025 16:25:15 -0700 Subject: [PATCH] Remove RCTUnsafeExecuteOnMainQueueSync from sample modules (#50149) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/50149 Let's just remove RCTUnsafeExecuteOnMainQueueSync from the examples. That way, people don't copy-paste it, thinking it's okay to use. Changelog: [Internal] Reviewed By: lyahdav Differential Revision: D71505823 fbshipit-source-id: f8d51b1a16bfdd6abdf62a6ba09ffff49d79a3f1 --- .../ios/ReactCommon/RCTSampleLegacyModule.mm | 15 +++------ .../ios/ReactCommon/RCTSampleTurboModule.mm | 33 ++++++++++--------- packages/rn-tester/package.json | 11 +++++-- 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/packages/react-native/ReactCommon/react/nativemodule/samples/platform/ios/ReactCommon/RCTSampleLegacyModule.mm b/packages/react-native/ReactCommon/react/nativemodule/samples/platform/ios/ReactCommon/RCTSampleLegacyModule.mm index 054aa5ca8d3..887aed735dc 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/samples/platform/ios/ReactCommon/RCTSampleLegacyModule.mm +++ b/packages/react-native/ReactCommon/react/nativemodule/samples/platform/ios/ReactCommon/RCTSampleLegacyModule.mm @@ -39,16 +39,11 @@ RCT_EXPORT_MODULE() - (NSDictionary *)getConstants { - __block NSDictionary *constants; - RCTUnsafeExecuteOnMainQueueSync(^{ - constants = @{ - @"const1" : @YES, - @"const2" : @(390), - @"const3" : @"something", - }; - }); - - return constants; + return @{ + @"const1" : @YES, + @"const2" : @(390), + @"const3" : @"something", + }; } // TODO: Remove once fully migrated to TurboModule. diff --git a/packages/react-native/ReactCommon/react/nativemodule/samples/platform/ios/ReactCommon/RCTSampleTurboModule.mm b/packages/react-native/ReactCommon/react/nativemodule/samples/platform/ios/ReactCommon/RCTSampleTurboModule.mm index 4813d906a5e..e19ec0485a5 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/samples/platform/ios/ReactCommon/RCTSampleTurboModule.mm +++ b/packages/react-native/ReactCommon/react/nativemodule/samples/platform/ios/ReactCommon/RCTSampleTurboModule.mm @@ -9,16 +9,19 @@ #import "RCTSampleTurboModulePlugin.h" #import +#import #import #import #import using namespace facebook::react; -@interface RCTSampleTurboModule () +@interface RCTSampleTurboModule () @end -@implementation RCTSampleTurboModule +@implementation RCTSampleTurboModule { + NSDictionary *_constants; +} // Backward-compatible export RCT_EXPORT_MODULE() @@ -29,6 +32,18 @@ RCT_EXPORT_MODULE() return YES; } +- (void)initialize +{ + UIScreen *mainScreen = UIScreen.mainScreen; + CGSize screenSize = mainScreen.bounds.size; + + _constants = @{ + @"const1" : @YES, + @"const2" : @(screenSize.width), + @"const3" : @"something", + }; +} + - (dispatch_queue_t)methodQueue { return dispatch_get_main_queue(); @@ -49,19 +64,7 @@ RCT_EXPORT_MODULE() - (NSDictionary *)getConstants { - __block NSDictionary *constants; - RCTUnsafeExecuteOnMainQueueSync(^{ - UIScreen *mainScreen = UIScreen.mainScreen; - CGSize screenSize = mainScreen.bounds.size; - - constants = @{ - @"const1" : @YES, - @"const2" : @(screenSize.width), - @"const3" : @"something", - }; - }); - - return constants; + return _constants; } // TODO: Remove once fully migrated to TurboModule. diff --git a/packages/rn-tester/package.json b/packages/rn-tester/package.json index 91b371db388..4cd6c089141 100644 --- a/packages/rn-tester/package.json +++ b/packages/rn-tester/package.json @@ -44,8 +44,15 @@ "javaPackageName": "com.facebook.fbreact.specs" }, "ios": { - "componentProvider": { - "RNTMyNativeView": "RNTMyNativeViewComponentView" + "modules": { + "SampleTurboModule": { + "unstableRequiresMainQueueSetup": true + } + }, + "components": { + "RNTMyNativeView": { + "className": "RNTMyNativeViewComponentView" + } } } },