Skip to content

Commit 633e114

Browse files
committed
Add platform-specific main thread schedulers for MAUI
Introduces AndroidMainThreadScheduler and AppleMainThreadScheduler properties for platform-specific main thread scheduling. Updates WithMauiScheduler to use the appropriate scheduler based on the target platform, improving UI thread handling for Android, iOS, macOS, and Mac Catalyst.
1 parent da99d4a commit 633e114

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/ReactiveUI.Maui/Builder/MauiReactiveUIBuilderExtensions.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@ public static class MauiReactiveUIBuilderExtensions
2020
/// </value>
2121
public static IScheduler MauiMainThreadScheduler { get; } = DefaultScheduler.Instance;
2222

23+
#if ANDROID
24+
/// <summary>
25+
/// Gets the scheduler that schedules work on the Android main (UI) thread.
26+
/// </summary>
27+
/// <remarks>Use this scheduler to execute actions that must run on the Android UI thread, such as
28+
/// updating user interface elements from background operations. This property is only available on Android
29+
/// platforms.</remarks>
30+
public static IScheduler AndroidMainThreadScheduler { get; } = HandlerScheduler.MainThreadScheduler;
31+
#endif
32+
33+
#if MACCATALYST || IOS || MACOS
34+
/// <summary>
35+
/// Gets the scheduler that schedules work on the Apple main (UI) thread.
36+
/// </summary>
37+
/// <remarks>Use this scheduler to execute actions that must run on the main UI thread of Apple platforms,
38+
/// such as updating user interface elements from background operations. This property is available on macOS, iOS,
39+
/// and Mac Catalyst platforms.</remarks>
40+
public static IScheduler AppleMainThreadScheduler { get; } = new WaitForDispatcherScheduler(static () => new NSRunloopScheduler());
41+
#endif
42+
2343
/// <summary>
2444
/// Configures ReactiveUI for MAUI platform with appropriate schedulers.
2545
/// </summary>
@@ -38,7 +58,9 @@ public static IReactiveUIBuilder WithMaui(this IReactiveUIBuilder builder, IDisp
3858
}
3959

4060
return builder
61+
#if !WINUI_TARGET
4162
.WithMauiScheduler()
63+
#endif
4264
.WithPlatformModule<Maui.Registrations>();
4365
}
4466

@@ -54,6 +76,12 @@ public static IReactiveUIBuilder WithMauiScheduler(this IReactiveUIBuilder build
5476
throw new ArgumentNullException(nameof(builder));
5577
}
5678

79+
#if ANDROID
80+
return builder.WithMainThreadScheduler(AndroidMainThreadScheduler);
81+
#elif MACCATALYST || IOS || MACOS
82+
return builder.WithMainThreadScheduler(AppleMainThreadScheduler);
83+
#else
5784
return builder.WithMainThreadScheduler(MauiMainThreadScheduler);
85+
#endif
5886
}
5987
}

0 commit comments

Comments
 (0)