Advertisement
Intermediate .NET MAUI, Networking & APIs

How do you keep API calls off the UI thread and update the UI safely?

Short answer

Await async HttpClient calls—they run I/O off the UI thread already. When you must marshal a result back to the UI from a non-UI context, use MainThread.BeginInvokeOnMainThread.

Save .NET MAUI mock interview
Advertisement
AModel answer
Await async HttpClient calls—they run I/O off the UI thread already. When you must marshal a result back to the UI from a non-UI context, use MainThread.BeginInvokeOnMainThread. Binding updates from async view-model code are marshaled by MAUI, but explicit UI mutations must be on the main thread.
var data = await _api.GetAsync();      // off UI thread
MainThread.BeginInvokeOnMainThread(() => Items.Add(data));
Real-world example
A background refresh fetches data without freezing the UI, then updates an observable collection on the main thread.
Likely follow-ups
  • When do you need MainThread.BeginInvokeOnMainThread? | Are bindings auto-marshaled?
Related topics