Xcode 14 “Publishing modifications from inside view updates just isn’t allowed, this may trigger undefined habits” – Donny Wals


Printed on: September 7, 2022

Expensive reader, for those who’ve discovered this web page you are in all probability encountering the error from the publish title. Let me begin by saying this publish does not give you a fast repair. As an alternative, it serves to point out you the occasion the place I bumped into this situation in Xcode 14, and why I consider this situation is a bug and never an precise situation. I’ve final examined this with Xcode 14.0’s Launch Candidate. I’ve filed suggestions with Apple, the suggestions quantity is FB11278036 in case you need to duplicate my situation.

A few of the SwiftUI code that I have been utilizing high quality for a very long time now has lately began arising with this purple warning.

Initially I believed that there was an opportunity that I used to be, in actual fact, doing one thing bizarre all alongside and I began chipping away at my undertaking till I had one thing that was sufficiently small to solely cowl a couple of traces, however nonetheless complicated sufficient to characterize the true world.

In my case, the problem occurred with the next code:

class SampleObject: ObservableObject {
    @Printed var publishedProp = 1337

    func mutate() {
        publishedProp = Int.random(in: 0...50)
    }
}

struct CellView: View {
    @ObservedObject var dataSource: SampleObject

    var physique: some View {
        VStack {
            Button(motion: {
                dataSource.mutate()
            }, label: {
                Textual content("Replace property")
            })

            Textual content("(dataSource.publishedProp)")
        }
    }
}

struct ContentView: View {
    @StateObject var dataSource = SampleObject()

    var physique: some View {
        Checklist {
            CellView(dataSource: dataSource)
        }
    }
}

This code actually does nothing outrageous or bizarre. A faucet on a button will merely mutate an @Printed property, and I anticipate the listing to replace. Nothing fancy. Nevertheless, this code nonetheless throws up the purple warning. Compiling this identical undertaking in Xcode 13.4.1 works high quality, and older Xcode 14 betas additionally do not complain.

At this level, it looks as if this is perhaps a bug in Checklist particularly as a result of altering the listing to a VStack or LazyVStack in a ScrollView doesn’t give me the identical warning. This tells me that there’s nothing essentially incorrect with the setup above.

One other factor that appears to work round this warning is to vary the kind of button that triggers the motion. For instance, utilizing a bordered button as proven under additionally runs with out the warning:

Button(motion: {
    dataSource.mutate()
}, label: {
    Textual content("Replace property")
}).buttonStyle(.bordered)

Or if you would like your button to seem like the default button fashion on iOS, you should use borderless:

Button(motion: {
    dataSource.mutate()
}, label: {
    Textual content("Replace property")
}).buttonStyle(.borderless)

It sort of seems like something besides a default Button in a Checklist is okay.

For these causes, I sadly can’t provide you with a correct repair for this situation. The issues I discussed are all workarounds IMO as a result of the unique code ought to work. All I can say is please file a suggestions ticket with Apple so we are able to hopefully get this fastened, documented, or in any other case defined. I will be requesting a code degree help ticket from Apple to see if an Apple engineer may help me determine this out.

As soon as I’ve a greater image of precisely what’s taking place I’ll replace this publish.

Leave a Reply