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

  Рет қаралды 11,224

Rebeloper - Rebel Developer

Rebeloper - Rebel Developer

Күн бұрын

Пікірлер: 68
@rebeloper
@rebeloper 2 жыл бұрын
How to use CoreData in SwiftUI 4 👉 kzbin.info/www/bejne/i32YnqiBpKyWas0
@smwoody88
@smwoody88 Жыл бұрын
First, thanks for the awesome video! In many of your replies you state that this is fixed in Xcode 14.1. Perhaps it has been addressed for some cases, but it seems to still happen using Xcode 14.2 when using MKCoordinateRegion as a Published variable. In addition, because MKCoordinateRegion is not Equatable (as pointed out by @martinmajewski in the comments), the solution you've provided doesn't work for this case. Here is a code snippet that has the same warning: ``` class ViewModel: ObservableObject { @Published var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 41, longitude: 14), span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1)) } struct MapView: View { @ObservedObject var viewModel = ViewModel() var body: some View { Map(coordinateRegion: $viewModel.region) } } ``` Do you have the same problem and have a suggestion to solve? Or do you believe it is still just a bug?
@rebeloper
@rebeloper Жыл бұрын
Yeah, I do have the same problem. What I ended up doing is use UIViewControllerRepresentable to grab it from UIKit. Here's the easiest realtime app I’ve ever built 👇 kzbin.info/www/bejne/qnekZGOmZtySrLM
@HeroTheOne
@HeroTheOne Жыл бұрын
Same problem here
@dannymeyer24
@dannymeyer24 9 ай бұрын
@@rebeloperI’m running into the same issue as OP, can you elaborate on how you did this? I clicked your link but that seems to be a video about fire base rather than maps. Thanks!
@StewartLynch
@StewartLynch 2 жыл бұрын
Another fantastic video Alex. So clear and well presented. Thanks. I am wondering if you can answer this question though. If you add a buttonStyle to your button that toggles the isPresented value, you no longer get the error. For example, I just added .buttonStyle(.borderedProminent) and the error no longer is presented.
@rebeloper
@rebeloper 2 жыл бұрын
The warnig is indeed not presented if you add .buttonStyle(.borderedProminent) but it will pop up when you're going to dismiss the sheet presented with the dismiss environment (even if you put a button style on it) Strange issue. We'll have to wait and see what comes out of beta.
@edwardhasted3849
@edwardhasted3849 Жыл бұрын
This is the only explanation I have been able to find on this subject - very many thanks.
@rebeloper
@rebeloper Жыл бұрын
You are welcome! Next Unlock the Power of Core Data Versioning! 👇 kzbin.info/www/bejne/iHXXfYqNpNhmmaM&ab_channel=Rebeloper-RebelDeveloper
@IceyMyst
@IceyMyst 2 жыл бұрын
Hi I believe this explanation may be incorrect as I'm able to replicate the warning in situations that don't use a binding. It appears that the warning is only displayed when using the default button styling. If you change the button styling to .plain, it seems to remove the warning. I suspect that the default button styling is triggering multiple view updates and somehow is running the logic within a view update inappropriately.
@rebeloper
@rebeloper 2 жыл бұрын
The warnig is indeed not presented if you add .buttonStyle(.borderedProminent) but it will pop up when you're going to dismiss the sheet presented with the dismiss environment (even if you put a button style on it) Strange issue. We'll have to wait and see what comes out of beta.
@boonyawatl
@boonyawatl 2 жыл бұрын
@@rebeloper My question is....should we apply the fix now or wait until Apple provides us with an update on this issue?
@ShooMPozitiV
@ShooMPozitiV 2 жыл бұрын
Just one video about this important thing. Thank you very much!!
@rebeloper
@rebeloper 2 жыл бұрын
Glad it was helpful! Share it with someone you think needs it. Next check out 6 State Mistakes Every Junior SwiftUI Developer Makes 👇 kzbin.info/www/bejne/b16VqI2pZ6dkadk
@johnappleseed6926
@johnappleseed6926 8 ай бұрын
Perfect title
@rebeloper
@rebeloper 8 ай бұрын
Thanks :) Next watch: SwiftUI Text - EVERYTHING You Need to Know 👇 kzbin.info/www/bejne/qHyol6Zrn5h0hKc&ab_channel=Rebeloper-RebelDeveloper
@paintopurpose
@paintopurpose Жыл бұрын
Can't we use pending changes instead? So in the view, when view rendering is happening, anytime you want to change an observed variable, you don't write to it directly...you'd have a struct that could have pending new values. Then after your view update has completed, simply trigger a func that will commit those pending changes to observed variables
@rebeloper
@rebeloper Жыл бұрын
I don't know how we could do that. Next check out SwiftUI Navigation for Dummies 👇 kzbin.info/www/bejne/eqbKc4l8f56diKs&ab_channel=Rebeloper-RebelDeveloper
@lincolndickerson1293
@lincolndickerson1293 Жыл бұрын
I have several views that have bindings to published vars that are acting strange which worked perfectly before XCode 14/iOS 16. No purple warnings just acting badly. Textfields only reflect one character then erase it when a second character is typed then may or may not show the third char. Never displays more than one. Which I can see as similar to the weird dismiss button behavior. This validates my suspicions that something pretty important changed. Things “seem” to work binding just one or two views levels deep. When there are several levels of subviews binding to a published var that is when things go wrong. Based on the structure of the data ,arrays of arrays, and the dynamic nature of the views based on the contents of those arrays binding to public vars worked very well. Hoping using the synching mechanism example presented here I will be able to adapt it work with Xcode’s current idiosyncrasies in my project. Thanks.
@rebeloper
@rebeloper Жыл бұрын
Most probably you have issues setting up your data handling. I recommend taking a look at this video first: kzbin.info/www/bejne/qXbclKCpf9OjrKM Next check out How I HACKED My Brain To Learn Coding 👇 kzbin.info/www/bejne/aKWYfI2inrGVZrM
@lincolndickerson1293
@lincolndickerson1293 Жыл бұрын
@@rebeloper if you were take the example you have shown here and passed the name through two or more views using bindings to create a complex view at the top you would see similar behavor to what you did with the dismiss button. The views I am working with worked fine before Xcode 14.
@rebeloper
@rebeloper Жыл бұрын
Have you tried not passing them along as a Binding but having an EnvironmentObject? Next check out Why You Should Use The SwiftUI Coordinator Pattern 👇 kzbin.info/www/bejne/l5Kvg4aKbNl7fLM
@lincolndickerson1293
@lincolndickerson1293 Жыл бұрын
@@rebeloper So when you have several many to one relationship being presented through ForEaches coordinating through an environment object is less than optimal but also on of the solutions I am sorting through
@ZorianaIvaniuk
@ZorianaIvaniuk 2 жыл бұрын
Amazing axplanation. This warnings is so annoying. But I don't understand why they removed binding. It's so useful. I hope it will be fixed in the next version of XCode
@rebeloper
@rebeloper 2 жыл бұрын
Agreed! It should have been fixed in iOS16.1. BTW Do you know how to properly name things in code? 👇 kzbin.info/www/bejne/n4myapaijdd1oJo
@apollocolorado
@apollocolorado Жыл бұрын
Great explanation. I have a question. In the beginning you say not to use a binding $ with a @Published property, but around 7:44 you use the line .onReceive(appService.$isPresented) { Why does that compile and not show the purple warning? Indeed .onReceive shows a compiler error without the $. Thanks!
@rebeloper
@rebeloper Жыл бұрын
I'm parsing it into a State variable. Btw this has been fixed in the next version of Xcode. Next Learn About One Fatal Programming Mistake: Stop Making it a Hobby! 👇 kzbin.info/www/bejne/gobWqpV8fahnedE&ab_channel=Rebeloper-RebelDeveloper
@lululucaschae
@lululucaschae Жыл бұрын
The warnings are gone again on iOS 16.2 - does that mean we don't need to worry about fixing it anymore?
@rebeloper
@rebeloper Жыл бұрын
Yes, it look like it has been fixed in iOS16.2 🎉 Also here are the Best Coding Habits to Adopt in 2023 👇 kzbin.info/www/bejne/boCZfmqlpJqlns0
@trevordevelops
@trevordevelops 2 жыл бұрын
To be clear, is this a bug or how we will have to handle bindings from now (iOS 16) and on?
@rebeloper
@rebeloper 2 жыл бұрын
Looks like this has been fixed in Xcode14.1 (iOS16.1) so it seems it was a bug. If you want to launch a business as a developer don't miss this video: The One-Person Business Model (How To Productize Yourself) 👇 kzbin.info/www/bejne/m3iZYWafppt6ptk
@falcosolitario
@falcosolitario 2 жыл бұрын
really thank you for you video. I was searching this kind of solution for 2 hours. But what do you mean "do not bind a published"???? is this for real? really worried about this
@rebeloper
@rebeloper 2 жыл бұрын
Let's wait for a few updates of Xcode. Maybe it's a bug. BTW here are 6 things I wish I knew about SwiftUI when I started 👇 kzbin.info/www/bejne/e4vdaoGCaa1ji8k
@abanoubghaly7685
@abanoubghaly7685 2 жыл бұрын
Thanks for sharing. You are awesome. do this lousy behavior cause a crash? Because my app is crashing (inconsistently) after installing the Xcode 14.0 so is the purple warning a crash causer or not?
@rebeloper
@rebeloper 2 жыл бұрын
Inconsistently it does crash. Mostly it's about navigation. I'll release a new video soon on how to do it so the warning goes away. Aprox. 2 weeks. Till than: 🎉 Just updated StoreKitPro 👉 kzbin.info/www/bejne/Y5jOp4V_i6qNqdU
@martinmajewski
@martinmajewski 2 жыл бұрын
This only works for Equatables! MapKit uses MKCoordinateRegion, which you cannot put into an ObservableObject class anymore, but you can also not use the .onChange modifier because MKCoordinateRegion is not Equatable.
@rebeloper
@rebeloper 2 жыл бұрын
Good point. It looks like this has been fixed in Xcode 14.1 Also here are 5 Tips for Writing BETTER For Loops in Swift 👇 kzbin.info/www/bejne/mX7LaaWrnbmMb80
@leunamvon2565
@leunamvon2565 Жыл бұрын
how can we do that same extension function for all type of variables? otherwise we have to create a extension function for every use case
@rebeloper
@rebeloper Жыл бұрын
This has been fixed already in an update, but I would say to use some sort of Generics 🤔 If you're a SwiftUI developer you will love: Unveiling The 10 Commandments of SwiftUI - #4 Will Surprise You! 👇 kzbin.info/www/bejne/aZ-rfGiZqbF2hKc
@VykingBoy
@VykingBoy Жыл бұрын
Even with this fix I still get these warnings ! Using Xcode 14.2 with Swift5.7.2 in a MapKit/CoreLocation app
@rebeloper
@rebeloper Жыл бұрын
Yes, I know. MapView still has this issue. Maybe use UIKit for this, that is what I do in one of my client apps. Next Unlock the Power of Core Data Versioning! 👇 kzbin.info/www/bejne/iHXXfYqNpNhmmaM&ab_channel=Rebeloper-RebelDeveloper
@hunterhillegas4988
@hunterhillegas4988 2 жыл бұрын
This seems like it has to be some sort of bug? It's a huge usability regression if not. I'll check the release candidate and if still an issue, talk to DTS.
@theo788
@theo788 2 жыл бұрын
Looks like it's still an issue in the RC...
@rebeloper
@rebeloper 2 жыл бұрын
RC still has this issue :( Next check out 6 State Mistakes Every Junior SwiftUI Developer Makes 👇 kzbin.info/www/bejne/b16VqI2pZ6dkadk
@santhoshVnair
@santhoshVnair 2 жыл бұрын
Great explanation and solution. Sounds like, this new warning is to protect the view from external dependency to a published variable - in order to limit uncontrolled view refresh. So, an improvement could be to throttle the View modifier that implements the sync.
@rebeloper
@rebeloper 2 жыл бұрын
Nice suggestion. This seems to be fixed in iOS16 though. BTW check out The BEST Side Hustles For Programmers in 2023 👇 kzbin.info/www/bejne/nGe7n4Fvd69-Z5I
@chrispugmire
@chrispugmire 4 ай бұрын
I think this is the wrong solution in some situations. In my case at least the problem was changing the variables at the wrong time. Just change them in the onappear function and the problem goes away. I don't claim to understand the issue but I hope this helps someone.
@rebeloper
@rebeloper 3 ай бұрын
Thanks for the input. Have you seen my latest video? 👉 NavigationStack vs. NavigationCoordinator: You Won’t Believe the Difference! kzbin.info/www/bejne/bZfFaHaOqd5on5Y
@anasahalabi2749
@anasahalabi2749 2 жыл бұрын
This is now how we are supposed to code in swiftUI, this takes the whole point of the MVVM architecture, Apple has to create a patch update for this right? if not then we are doomed I believe.
@rebeloper
@rebeloper 2 жыл бұрын
Still there in Xcode14 :( Let's wait and see. BTW here are 6 things I wish I knew about SwiftUI when I started 👇 kzbin.info/www/bejne/e4vdaoGCaa1ji8k
@Arthur-eg7uy
@Arthur-eg7uy 2 жыл бұрын
Life saver video!
@rebeloper
@rebeloper 2 жыл бұрын
Glad it helped! BTW this seems to be saved in Xcode 14.1 I guess we'll see. Next check out these Top 10 SwiftUI One Liners That Will Blow Your Mind 👇 kzbin.info/www/bejne/nXWziqp5gK-jm7M
@Arthur-eg7uy
@Arthur-eg7uy 2 жыл бұрын
@@rebeloper actually no Xcode 14.1 beta 3 still has this issue! I worked on it today!
@rebeloper
@rebeloper 2 жыл бұрын
Thanks for letting me know. Use the "hack" provided :) Check out How I CODE For HOURS With NO Breaks 👇 kzbin.info/www/bejne/o4iveqWcl9h3oK8
@dugrut1325
@dugrut1325 Жыл бұрын
I get this error with Map(..., I've tried many things to stop or reduce the errors, It had nothing to due with changing @Published values
@rebeloper
@rebeloper Жыл бұрын
If you need to I can take a look at your code on a mentoring session. Book your call here: rebeloper.com/mentoring BTW check out my lates video 👉 2023's Must-Have Xcode Extensions: Revealed Here! 👇 kzbin.info/www/bejne/amKXaoWraa1nmKs
@thientoan0101
@thientoan0101 11 ай бұрын
awesome
@rebeloper
@rebeloper 11 ай бұрын
Glad you liked it! Now get ChatGPT 4 for Free in VSCode for iOS and macOS app development 👇 kzbin.info/www/bejne/b6HIlJtqnruHjbc&ab_channel=Rebeloper-RebelDeveloper
@nickpavlov4012
@nickpavlov4012 Жыл бұрын
Wait, I thought @MainActor fix that no? What is difference between @MainActor and what he did?
@rebeloper
@rebeloper Жыл бұрын
Well with iOS17 we will not have to worry about this at all :) Xcode 15 Beta: How To Download And Install 👇 www.youtube.com/watch? v=4gwW9pq_BT8&list=PL_csAAO9PQ8agVtPZVAQFhsexNJBPSteh&ab_channel=Rebeloper-RebelDeveloper
@nickpavlov4012
@nickpavlov4012 Жыл бұрын
@@rebeloper thanks for respond Rebeloper . Wow really? Thanks for info I’ll check it out
@rebeloper
@rebeloper Жыл бұрын
Welcome! Next watch What's New in Xcode 15 Beta 👇 kzbin.info/www/bejne/gHrKdnWCf5t2a6M&ab_channel=Rebeloper-RebelDeveloper
@perhapsyes2493
@perhapsyes2493 2 жыл бұрын
I regret switching our app to SwiftUI. So. Fucking. Much.
@rebeloper
@rebeloper 2 жыл бұрын
This seems to be fixed in Xcode 14.1 beta 3 :)) Next: Make Sure You Always Get the Best Error Alerts! 👇 kzbin.info/www/bejne/jIjKi2xve7moeLc
@victorchoi38
@victorchoi38 Жыл бұрын
I don't like this fix. Apple must fix this.
@rebeloper
@rebeloper Жыл бұрын
They already did :) Next watch Master Keychain Access Like a Pro using SwiftUI Property Wrappers 👇 kzbin.info/www/bejne/eGirc4CPeqqNqsU&ab_channel=Rebeloper-RebelDeveloper
@hypernikomen
@hypernikomen Жыл бұрын
For me, the solution was DispatchQueue.main.async: `DispatchQueue.main.async { viewModel.updateMyPublishedProperty(with: value) }` (Xcode 14.2).
@rebeloper
@rebeloper Жыл бұрын
Thank you for sharing :) Next find out the BEST WAY to Read and Understand Code 👇 kzbin.info/www/bejne/ZpzLo5Z8o9-bptU&ab_channel=Rebeloper-RebelDeveloper
Make MONEY with Swift StoreKit with iOS16!
27:19
Rebeloper - Rebel Developer
Рет қаралды 1,2 М.
6 State Mistakes Every Junior SwiftUI Developer Makes
18:06
Rebeloper - Rebel Developer
Рет қаралды 4 М.
How many people are in the changing room? #devil #lilith #funny #shorts
00:39
Увеличили моцареллу для @Lorenzo.bagnati
00:48
Кушать Хочу
Рет қаралды 8 МЛН
Turn Off the Vacum And Sit Back and Laugh 🤣
00:34
SKITSFUL
Рет қаралды 7 МЛН
The Ultimate Sausage Prank! Watch Their Reactions 😂🌭 #Unexpected
00:17
La La Life Shorts
Рет қаралды 9 МЛН
Why You Should Use The SwiftUI Coordinator Pattern
18:45
Rebeloper - Rebel Developer
Рет қаралды 16 М.
NVIDIA’s New AI: Stunning Voice Generator!
6:21
Two Minute Papers
Рет қаралды 100 М.
Creating a Custom Framework in Swift in 2022
26:54
Rebeloper - Rebel Developer
Рет қаралды 15 М.
.NET and C# are in trouble. Here is what I'd do.
10:57
Ed Andersen
Рет қаралды 107 М.
I never understood why you can't go faster than light - until now!
16:40
FloatHeadPhysics
Рет қаралды 4 МЛН
SwiftUI Drag and Drop with Transferable Custom Object
26:33
Sean Allen
Рет қаралды 24 М.
Master SwiftUI View States: The Secret to Seamless Apps!
20:25
Rebeloper - Rebel Developer
Рет қаралды 676
How many people are in the changing room? #devil #lilith #funny #shorts
00:39