From a8e080884947f6249083878ae0ed849ef4a807cd Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Sat, 4 Oct 2025 10:28:03 +0200 Subject: [PATCH] chore: fix warning about appearance being called off main thread --- .../React/CoreModules/RCTAppearance.mm | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/react-native/React/CoreModules/RCTAppearance.mm b/packages/react-native/React/CoreModules/RCTAppearance.mm index 4053d54a9e1a12..c64b2b4e6f74e7 100644 --- a/packages/react-native/React/CoreModules/RCTAppearance.mm +++ b/packages/react-native/React/CoreModules/RCTAppearance.mm @@ -98,9 +98,12 @@ void RCTUseKeyWindowForSystemStyle(BOOL useMainScreen) return RCTAppearanceColorSchemeLight; } - appearance = appearance ?: [NSApp effectiveAppearance]; + __block NSAppearance *resolvedAppearance = appearance; + RCTUnsafeExecuteOnMainQueueSync(^{ + resolvedAppearance = resolvedAppearance ?: [NSApp effectiveAppearance]; + }); - NSAppearanceName appearanceName = [appearance bestMatchFromAppearancesWithNames:@[NSAppearanceNameAqua, NSAppearanceNameDarkAqua]]; + NSAppearanceName appearanceName = [resolvedAppearance bestMatchFromAppearancesWithNames:@[NSAppearanceNameAqua, NSAppearanceNameDarkAqua]]; return appearances[appearanceName] ?: RCTAppearanceColorSchemeLight; } #endif // macOS] @@ -119,7 +122,14 @@ - (instancetype)init UITraitCollection *traitCollection = [RCTTraitCollectionProxy sharedInstance].currentTraitCollection; _currentColorScheme = RCTColorSchemePreference(traitCollection); #else // [macOS - NSAppearance *appearance = RCTSharedApplication().appearance; + // Initialize with a concrete NSAppearance, fetched safely on the main thread. + __block NSAppearance *appearance = nil; + RCTUnsafeExecuteOnMainQueueSync(^{ + appearance = RCTSharedApplication().appearance ?: [NSApp effectiveAppearance]; + if (!appearance) { + appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua]; + } + }); _currentColorScheme = RCTColorSchemePreference(appearance); #endif // macOS] [[NSNotificationCenter defaultCenter] addObserver:self @@ -173,7 +183,7 @@ - (dispatch_queue_t)methodQueue RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString *, getColorScheme) { if (!sIsAppearancePreferenceSet) { -#if !TARGET_OS_OSX // [macOS +#if !TARGET_OS_OSX // [macOS] UITraitCollection *traitCollection = [RCTTraitCollectionProxy sharedInstance].currentTraitCollection; _currentColorScheme = RCTColorSchemePreference(traitCollection); #else // [macOS @@ -228,4 +238,4 @@ - (void)invalidate Class RCTAppearanceCls(void) { return RCTAppearance.class; -} \ No newline at end of file +}