Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

SwiftUI warns that publishing changes from within view updates is not allowed

ErrorPublishing changes from within view updates is not allowed, this will cause undefined behavior.

Short answer

SwiftUI prints this when state or a published property changes while it is still computing a view's body. A body must only describe the view. Move the write into an event closure such as onAppear, onChange or a button action, make view-model members that body reads free of writes, and send UIKit changes back through a Coordinator.

  • Applies to Xcode 26, Swift 6, SwiftUI, iOS 26
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find every place a screen writes to state. The warning has a write behind it, so list the assignments in the view file and in the view model it uses, then read each one for the moment it runs.

    grep -n "viewModel\.[A-Za-z]* = \|self\.[A-Za-z]* = " Sources/Screens/HomeView.swift
    grep -n "@Published\|@State\|@Binding" Sources/Screens/*.swift
  2. 2Match what you see, then jump to its fix

    1. 1A line inside body (or inside a view builder it contains) assigns to a @State, @Binding or @Published value.The body of a view assigns to state or a published propertyFix
    2. 2Body itself contains no assignment, but a computed property or method it calls assigns to a @Published property.A view-model member that body reads changes published state as a side effectFix
    3. 3The warning shows up on a screen that wraps a UIKit view (a map, a text view, a picker) and the assignment to a binding sits in the representable's update method.A UIViewRepresentable writes back into SwiftUI from updateUIView or makeUIViewFix

What does the error look like?

  • Publishing changes from within view updates is not allowed, this will cause undefined behavior.

Cause 1 of 3

The body of a view assigns to state or a published property

1Check

Look for an assignment that sits directly in body, before the view that is returned. That code runs every time SwiftUI computes the body, which is a view update. Apple's data-flow session says a body should be a pure function that only creates the view description and returns.

var body: some View {
    viewModel.title = "Home"        // a write while the body is being computed
    return Text(viewModel.title)
}

2Fix

Make the write a reaction to an event instead of part of the description. Apple's example loads data from onAppear; onChange(of:) is the modifier for a side effect that follows a value change, and a Button action is the place for a tap.

var body: some View {
    Text(viewModel.title)
        .onAppear { viewModel.title = "Home" }
        .onChange(of: viewModel.query) {
            viewModel.search()
        }
}

3Verify

Run the app, open the screen and watch the Xcode console and the Issue navigator. The warning should not appear when the screen is drawn.

# Xcode: Product > Run, open the screen, then check the debug console and the Issue navigator

Confirmed by developer.apple.com

Did this fix it?

Cause 2 of 3

A view-model member that body reads changes published state as a side effect

1Check

Open each view-model property and function that body reads. A getter that stores its result, clears an error or flips a loading flag is a write that runs during the update.

// view model
var visibleItems: [Item] {
    filtered = items.filter { $0.isActive }   // assigns to a @Published property
    return filtered
}

2Fix

Return the value without storing it, so reading it changes nothing. If the result has to be kept as state, update it where its input changes, from an onChange closure on the view.

var visibleItems: [Item] {
    items.filter { $0.isActive }
}

// or keep a stored copy and refresh it on an event (Item must be Equatable)
.onChange(of: viewModel.items) {
    viewModel.refreshFiltered()
}

3Verify

Open the screen again. Reading the view model during a redraw must no longer produce the warning.

# Xcode: Product > Run, scroll and filter the list, then check the debug console

Confirmed by developer.apple.com

Did this fix it?

Cause 3 of 3

A UIViewRepresentable writes back into SwiftUI from updateUIView or makeUIView

1Check

updateUIView is documented as the method that updates the UIKit view with new information from SwiftUI. An assignment to a @Binding in it pushes a change the other way while SwiftUI is updating.

func updateUIView(_ uiView: UITextView, context: Context) {
    text = uiView.text          // writes the binding during an update
}

2Fix

Keep updateUIView one-directional: SwiftUI state goes into the UIKit view. Changes that start in UIKit travel through the Coordinator, which Apple describes as the object that forwards target-action and delegate messages to SwiftUI.

func makeCoordinator() -> Coordinator { Coordinator(text: $text) }

func updateUIView(_ uiView: UITextView, context: Context) {
    if uiView.text != text { uiView.text = text }
}

final class Coordinator: NSObject, UITextViewDelegate {
    var text: Binding<String>
    init(text: Binding<String>) { self.text = text }
    func textViewDidChange(_ textView: UITextView) {
        text.wrappedValue = textView.text
    }
}

3Verify

Type into or move the wrapped UIKit view and check that the SwiftUI value follows, and that the console stays quiet.

# Xcode: Product > Run, interact with the wrapped view, watch the debug console

Confirmed by developer.apple.com

Did this fix it?

How do I stop it coming back?

  • Treat body as a description of the screen: it creates views and returns, with no assignments and no calls that change state.
  • Do not assume when or how often body runs; Apple's guidance is to avoid building logic on that.
  • Keep derived values as computed properties that return a result, rather than stored properties that code has to keep in step.
  • In a UIViewRepresentable, let updateUIView push SwiftUI state in and let the Coordinator carry changes out.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

InstanceID token() no longer works in a Swift Firebase app

Instance ID calls fail to compile after upgrading Firebase to 8.0.0 or later

Firebase removed the Instance ID dependency from Messaging in 8.0.0, so InstanceID.instanceID() calls no longer belong in a current app.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.