|
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.
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 Edit 2: FIXED This seems to be fixed on iOS 16.2 (It was never an issue on iPad/macOS) |
|
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. |
|
Yes. Fix it The add this will push the changes that it make to the main queue where UI changes are made. |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and more! Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
Thanks for the replies. However, both @NigelGee and @Hatsushira are referring to 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 |
|
If the 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 |
|
@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
Parent View
Child View (Upon clicking the button that mutates the VM's published property, you get the purple warning)
|
|
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 |
|
|
|
@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) |
|
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! |
|
@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 |
|
|
|
|
|
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". |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and more!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.