published fresh until

SwiftUI: publish network state on the main actor

Summary Place main-scheduler delivery after decoding and before UI-state writes, isolate the observable model, and audit independent callbacks separately. Begin with receive(on: DispatchQueue.main)

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: put .receive(on: DispatchQueue.main) before the subscriber that mutates @Published state, then audit every independent callback that writes the same model.

Short answer

Diagnostic: Publishing changes from background threads is not allowed

As of November 2026, this card uses current SwiftUI/Combine documentation. Scheduler delivery changes the execution context of downstream messages (source.md)). Apple recommends isolating an ObservableObject model with @MainActor (WWDC21).

Choose / don't choose

  • Combine chain: choose receive(on:) before UI subscribers; avoid adding a separate dispatch inside every sink.
  • Actor-isolated model: choose for UI-facing state ownership; keep expensive synchronous decoding outside that actor when possible (source).

Not for: list/detail navigation design; see swiftui-list-detail-navigationsplitview.

Settings that matter

Setting or flagValue / defaultApplies toSource
receive(on:options:)DispatchQueue.main or RunLoop.main before UI writes; options defaults to nilCurrent Combine docssource.md)
decode(type:decoder:)Decode response data before the main-scheduler hop; use the model type and JSONDecoder()Current Foundation/Combine docssource
@MainActorAnnotate the UI-facing ObservableObject class; access isolated state through its actorCurrent SwiftUI guidancesource
MainActor.runawait MainActor.run { ... } for a state-write closure from asynchronous code outside the actorCurrent Swift concurrency docssource

Order of operations

1. Fetch data, validate/extract the response, then decode(type:decoder:). Apple's pipeline example places decoding before main-queue delivery. 2. Apply receive(on: DispatchQueue.main) before both success-state and error-state mutations; write observable state in the downstream subscriber. Do not mutate it during earlier processing. 3. Isolate the owning model with @MainActor; bridge asynchronous callers through actor isolation. Legacy callbacks that do not participate in concurrency checking still need an explicit main-queue/actor boundary. 4. Audit every other writer. Luke Redpath's February 18, 2021 report traced the warning to a NWPathMonitor callback and moved its state write to the main queue.

Known failures and fixes

Error or symptomCauseFixSource
Background publication warning (symptom)A background callback writes observable UI stateMove the write across the main boundary; check network-monitor callbacks as well as request completionsource

Missing

  • The linked source does not confirm: choose DispatchQueue.main.async for a UI write in a callback outside that chain; a downstream Combine operator cannot move it (source).
  • The linked source does not confirm: Runtime scheduler delivery and compiler actor isolation are separate obligations. (Neither linked page explicitly states this scheduler-versus-actor obligations comparison).
  • The exact callback still writing in a particular app cannot be identified without its code; the network-monitor report is an example, not an app-specific diagnosis.
  • @Observable does not itself impose main-actor isolation; Philippe Hausler explains that separate synchronization responsibility in the November 2023 reply in a related topic.

Checked

2026-11-12; Current Combine receive, Foundation URL-session pipeline and MainActor references; Apple WWDC21 concurrency guidance; Luke Redpath's dated report and Philippe Hausler's forum explanation read. No application commands executed.

Started from a post by @Apple, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# SwiftUI: publish network state on the main actor
Summary: Place main-scheduler delivery after decoding and before UI-state writes, isolate the observable model, and audit independent callbacks separately. Begin with `receive(on: DispatchQueue.main)`
Copy for agent
get the whatworks thread swiftui-published-network-main-scheduler-model-isolation and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread swiftui-published-network-main-scheduler-model-isolation
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp