WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: [EDIT] Should I fix "Publishing changes from within view updates is not allowed"?

Forums > SwiftUI

Hi, I'm sure many of you are experiencing the same issue - but I don't see any mentions here so I thought I'd start the discussion.

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

is a purple warning on Xcode 14 when you try to change a @Published property from other views. All of my warnings come from mutating a published varaible using Buttons. (I won't post any code since there are a lot of examples out there - Example 1, Example 2)

All the solutions/workarounds seem very tedious and unncessary, and I'm very much overwhelemed to fix ~100 warnings I have on my project. I know Apple often take back these warnings when they release new versions, and there seem to be quite a few people considering this merely a bug. I was ready to publish my app on Testflight, but was wondering if I should I fix these warnings before, or just leave it and see what happens with Xcode/Swift updates?

Edit

Seems like I need clarify that this warning is DIFFERENT than the background thread publishing warning, which is Publishing changes from background threads is not allowed

Edit 2: FIXED

This seems to be fixed on iOS 16.2 (It was never an issue on iPad/macOS)

2      

Not every instance of this warning is a bug in Xcode. Usually, the message comes when you change the UI and you are not on the main queue doing so. So unless we see your code we can't help you with this message because we don't know what you're doing.

2      

Yes. Fix it

The add @MainActor to the class that the @Published is changing this usually solve it.

this will push the changes that it make to the main queue where UI changes are made.

How to use @MainActor to run code on the main queue

3      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Thanks for the replies. However, both @NigelGee and @Hatsushira are referring to Publishing changes from background threads is not allowed , which I believe is a totally different warning.

I didn't provide any code because it's almost identical to the two examples I linked in my original post.

The best explanation and workaround I've seen so far is by Rebeloper and he has a video on this, but like I said in the original post, I still find it very tedious and unnecessary - since it was never an issue until xcode 14. https://www.youtube.com/watch?v=3a7tuhVpoTQ

2      

If the View that is using the Published value, is also trying to modify it (for example the action performed when using a Button) which will happen first, as you will be using closures, then the warning is given.

Yes, something will be displayed, and the app should not crash as a result, but what is displayed may not be what you intended.

You could try putting the Published value into a State for the View.

2      

@Greenamberred - Thank you very much. I'm afraid that is the workaround I have to take but with so many warnings it felt very overwhelming 😂

Has Swift changed the way data is handled? Or has this always been a bad way to use @Published but they never cared until now? I don't know why it was never a problem but it's suddenly becoming an issue ..

For others who haven't expeirenced this warning yet, here's a minimal reproducible example.

A Manager/VM class with a published variable

class SampleManager: ObservableObject {
    @Published var isSheetShowing: Bool = false
}

Parent View

struct ContentView: View {

    @StateObject var sampleManager = SampleManager()

    var body: some View {
        Button("Open Sheet") {
            sampleManager.isSheetShowing.toggle()
        }
        .sheet(isPresented: $sampleManager.isSheetShowing) {
            CountView()
        }
        .environmentObject(sampleManager)
        .padding()
    }
}

Child View (Upon clicking the button that mutates the VM's published property, you get the purple warning)

struct CountView: View {

    @EnvironmentObject var sampleManager: SampleManager

    var body: some View {
        Button("Hide Sheet") {
            sampleManager.isSheetShowing.toggle()
        }
    }
}

2      

From what I understand the potential problems have always been there, it is just that now Apple is warning you about it, so you can decide what steps you want to take to prevent possible problems with the View displayed.

2      

@lululucaschae

From your example code in Xcode 14.2 do not get any warnings

2      

@NigelGee Make sure you run it in an iOS16 device or simulator... This warning "Publishing changes from within view updates is not allowed, this will cause undefined behavior" does not appear using iOS 14/15 in my Mac (running with Xcode 14.2)

2      

I was able to reproduce with Xcode 14.3 using the Bucketlist project from HWS SwiftUI book as well as with my own similar implementation. @MainActor does appear to make this safe though!

2      

@mightyquinn408 Could you explain more on where the addition of @MainActor fixed the problem? I'm currently stuck with this error on the BucketList project. Running XCode 14.3 and iOS 16.4 in the simulator

2      

Hi. I am getting this warning now also as I am finishing up the BucketList project. Is there a way to trace where this error is coming from (i.e. which update is triggering the error)?

2      

Also getting this warning in the BucketList project.

2      

I found a thread about this issue on Apple forums, they mentioned that the problem is caused by the combination of Map() and MapAnnotation(), and someone also mentioned that this issue was already reported to Apple and they replied: "Potential fix identified - For a future OS update".

https://developer.apple.com/forums/thread/718697

2      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.