From f81fd3641555bbac5ca58eaf9a266c481ff47e14 Mon Sep 17 00:00:00 2001 From: redth Date: Wed, 11 Mar 2026 13:03:21 -0400 Subject: [PATCH] Fix UIKit thread error in AppInfo and DeviceDisplay handlers AppInfo.Current (RequestedTheme, RequestedLayoutDirection) and DeviceDisplay.MainDisplayInfo access UIKit APIs that must be called on the main thread. The HTTP server handlers run on a background thread, causing 'UIKit Consistency error' on iOS. Wrap both handlers in MainThread.InvokeOnMainThreadAsync() to dispatch to the UI thread before accessing these APIs. Fixes #34 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../DevFlowAgentService.cs | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/src/MauiDevFlow.Agent.Core/DevFlowAgentService.cs b/src/MauiDevFlow.Agent.Core/DevFlowAgentService.cs index 86a0076..ef2d39b 100644 --- a/src/MauiDevFlow.Agent.Core/DevFlowAgentService.cs +++ b/src/MauiDevFlow.Agent.Core/DevFlowAgentService.cs @@ -3638,24 +3638,27 @@ private Task HandleSecureStorageClear(HttpRequest request) // ── Platform info endpoints ── - private Task HandlePlatformAppInfo(HttpRequest request) + private async Task HandlePlatformAppInfo(HttpRequest request) { try { - var info = AppInfo.Current; - return Task.FromResult(HttpResponse.Json(new + return await MainThread.InvokeOnMainThreadAsync(() => { - name = info.Name, - packageName = info.PackageName, - version = info.VersionString, - buildNumber = info.BuildString, - requestedTheme = info.RequestedTheme.ToString(), - requestedLayoutDirection = info.RequestedLayoutDirection.ToString(), - })); + var info = AppInfo.Current; + return HttpResponse.Json(new + { + name = info.Name, + packageName = info.PackageName, + version = info.VersionString, + buildNumber = info.BuildString, + requestedTheme = info.RequestedTheme.ToString(), + requestedLayoutDirection = info.RequestedLayoutDirection.ToString(), + }); + }); } catch (Exception ex) { - return Task.FromResult(HttpResponse.Error($"Failed to get app info: {ex.Message}")); + return HttpResponse.Error($"Failed to get app info: {ex.Message}"); } } @@ -3681,24 +3684,27 @@ private Task HandlePlatformDeviceInfo(HttpRequest request) } } - private Task HandlePlatformDeviceDisplay(HttpRequest request) + private async Task HandlePlatformDeviceDisplay(HttpRequest request) { try { - var display = DeviceDisplay.MainDisplayInfo; - return Task.FromResult(HttpResponse.Json(new + return await MainThread.InvokeOnMainThreadAsync(() => { - width = display.Width, - height = display.Height, - density = display.Density, - orientation = display.Orientation.ToString(), - rotation = display.Rotation.ToString(), - refreshRate = display.RefreshRate, - })); + var display = DeviceDisplay.MainDisplayInfo; + return HttpResponse.Json(new + { + width = display.Width, + height = display.Height, + density = display.Density, + orientation = display.Orientation.ToString(), + rotation = display.Rotation.ToString(), + refreshRate = display.RefreshRate, + }); + }); } catch (Exception ex) { - return Task.FromResult(HttpResponse.Error($"Failed to get display info: {ex.Message}")); + return HttpResponse.Error($"Failed to get display info: {ex.Message}"); } }