Sunday, October 15, 2023
HomeiOS DevelopmentXcode 14 “Publishing adjustments from inside view updates just isn't allowed, this...

Xcode 14 “Publishing adjustments from inside view updates just isn’t allowed, this can trigger undefined conduct” – Donny Wals


Printed on: September 7, 2022

Pricey reader, in case you’ve discovered this web page you are in all probability encountering the error from the put up title. Let me begin by saying this put up does not give you a fast repair. As an alternative, it serves to point out you the occasion the place I bumped into this concern in Xcode 14, and why I consider this concern is a bug and never an precise concern. 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 concern.

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

Initially I believed that there was an opportunity that I used to be, the truth is, doing one thing bizarre all alongside and I began chipping away at my venture till I had one thing that was sufficiently small to solely cowl a number of traces, however nonetheless advanced sufficient to signify the actual 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 record to replace. Nothing fancy. Nonetheless, this code nonetheless throws up the purple warning. Compiling this identical venture in Xcode 13.4.1 works effective, and older Xcode 14 betas additionally do not complain.

At this level, it looks as if this could be a bug in Checklist particularly as a result of altering the record to a VStack or LazyVStack in a ScrollView doesn’t give me the identical warning. This tells me that there’s nothing basically fallacious 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 beneath additionally runs with out the warning:

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

Or if you need your button to appear to be the default button fashion on iOS, you should use borderless:

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

It type of appears like something besides a default Button in a Checklist is okay.

For these causes, I sadly can’t offer you a correct repair for this concern. 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 will hopefully get this fastened, documented, or in any other case defined. I will be requesting a code degree assist ticket from Apple to see if an Apple engineer may also help me determine this out.

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



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments