SwiftUI warns that publishing changes from within view updates is not allowed
Publishing 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.
Fix it fast
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/*.swift2Match what you see, then jump to its fix
- 1
A 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
Body 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
The 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
- 1
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 navigatorConfirmed by developer.apple.com
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 consoleConfirmed by developer.apple.com
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 consoleConfirmed by developer.apple.com
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
UserDefaults logs 'Failed to read values in CFPrefsPlistSource' for an app group
Settings saved by the app are not visible to its extension, widget or sibling app that share the group
This log comes from the preferences system when UserDefaults is opened for an app group's suite.
3 causes, each with a fixMobile apps
iOS app crashes: Google Maps SDK must be initialized via provideAPIKey
Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...] prior to use
The Maps SDK throws this when a Maps object is created before GMSServices.provideAPIKey has run.
3 causes, each with a fixMobile apps
UIKit app crashes because it cannot find the storyboard
Could not find a storyboard named 'Main' in bundle NSBundle
UIStoryboard throws this exception when no storyboard file with that name exists in the bundle it searches.
3 causes, each with a fixMobile apps
Firebase on iOS asks for the FCM token before APNs has given one
Be sure to re-retrieve the FCM token once the APNS device token is set.
The Firebase Messaging SDK needs the APNs device token from iOS before it hands out an FCM token.
3 causes, each with a fixMobile apps
A test push from the Firebase console never shows up on the iPhone
didReceiveRegistrationToken prints a token, yet no notification arrives
When a console message is accepted but the iPhone stays silent, the break is between Firebase and APNs or inside the app.
4 causes, each with a fixMobile 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