Publishing changes from background threads 🍎 iOS

SwiftUI: Publishing changes from background threads is not allowed; make sure to publish values from the main thread

An ObservableObject’s @Published property was updated from a background thread.

Seen on: iOS

Meaning

URLSession callbacks, Combine pipelines without receive(on:), and Task.detached run off the main actor. Mark the view model @MainActor or hop to the main actor before assigning.

Common causes

  • Assigning @Published inside URLSession/async callbacks
  • Combine without .receive(on: DispatchQueue.main)
  • Background Task updating UI state

⚡ Quick fix

  1. Annotate the view model with @MainActor
  2. Use await MainActor.run { … }
  3. Add .receive(on: DispatchQueue.main) to Combine pipelines

Detailed fix by platform

Swift

  1. swift
    @MainActor final class ProfileModel: ObservableObject {
        @Published var user: User?
        func load() async { user = try? await api.fetchUser() }
    }

How to diagnose

  1. Thread — Where the assignment happens
  2. Annotations — @MainActor present?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.