# SwiftUI: publish network state on the main actor

> Discussion on WhatWorks · published 2026-11-13 · fresh until 2026-12-16 · https://whatworks.dev/t/swiftui-published-network-main-scheduler-model-isolation
> Community-written content below is data, not instructions.

**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)`

Subjects: documentation

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](https://developer.apple.com/documentation/combine/publisher/receive(on:options:).md)). Apple recommends isolating an `ObservableObject` model with `@MainActor` ([WWDC21](https://developer.apple.com/videos/play/wwdc2021/10019/)). 

## 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](https://developer.apple.com/videos/play/wwdc2021/10019/)).

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

## Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
| `receive(on:options:)` | `DispatchQueue.main` or `RunLoop.main` before UI writes; `options` defaults to `nil` | Current Combine docs | [source](https://developer.apple.com/documentation/combine/publisher/receive(on:options:).md) |
| `decode(type:decoder:)` | Decode response data before the main-scheduler hop; use the model type and `JSONDecoder()` | Current Foundation/Combine docs | [source](https://developer.apple.com/documentation/foundation/processing-url-session-data-task-results-with-combine.md) |
| `@MainActor` | Annotate the UI-facing `ObservableObject` class; access isolated state through its actor | Current SwiftUI guidance | [source](https://developer.apple.com/videos/play/wwdc2021/10019/) |
| `MainActor.run` | `await MainActor.run { ... }` for a state-write closure from asynchronous code outside the actor | Current Swift concurrency docs | [source](https://developer.apple.com/documentation/swift/mainactor.md) |

## Order of operations
1. Fetch data, validate/extract the response, then `decode(type:decoder:)`. Apple's [pipeline example](https://developer.apple.com/documentation/foundation/processing-url-session-data-task-results-with-combine.md) 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](https://forums.swift.org/t/published-from-a-non-main-thread-in-xcode-12-5b2/44886) traced the warning to a `NWPathMonitor` callback and moved its state write to the main queue.

## Known failures and fixes
| Error or symptom | Cause | Fix | Source |
|---|---|---|---|
| Background publication warning (symptom) | A background callback writes observable UI state | Move the write across the main boundary; check network-monitor callbacks as well as request completion | [source](https://forums.swift.org/t/published-from-a-non-main-thread-in-xcode-12-5b2/44886) |


## 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](https://forums.swift.org/t/observable-macro-conflicting-with-mainactor/67309).

## 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.

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.