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

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ส.ค. 2024
  • Publishing changes from within view updates is not allowed, this will cause undefined behaviour
    Escape Tutorial Hell 👉 rebeloper.com/mentoring
    Want to learn about observable object and published in swiftui? Want to learn observable object and published in swiftui for beginners? Are you looking for a good app development video and swiftui tutorial? Have you ever wondered what is observable object swiftui and how to learn ios development? Well you're in luck! Because in this swiftui published for beginners tutorial we will core all of that! In this observable object in swiftui tutorial you will discover swiftui observable object. I will give some tips on observable object in swift, what is published swiftui and ios in general. In this programming tutorial I'll guide you through observableobject swiftui. In this xcode tutorial we will talk about published swiftui. ​Don’t forget to subscribe to my TH-cam channel for more swift ui published tutorial and swiftui observable object for beginners videos.
    Rebeloper is here to help you in learning observable object and published in swiftui.
    Let's dive in!
    Don’t forget to download the resources:
    → store.rebeloper.com/youtube-c...
    CHAPTERS:
    _____
    00:00 INTRO
    00:22 MENTROING
    00:44 SWIFTUI4 WARNING
    01:24 RECREATING THE PURPLE WARNING
    02:06 UNDERSTANDING THE PROBLEM
    03:52 PUBLISHED IN SWIFTUI 4
    04:54 FIX THE PURPLE WARNING
    06:04 USING PUBLISHED WITH STATE
    09:45 REFACTORING THE CODE
    13:23 WHERE TO GO NEXT
    **VIDEOS I RECOMMEND**
    → PLAYLIST:
    Super Summer Sale:
    • Best iOS Development T...
    DO YOU WANT TO BE MY APPRENTICE?
    HIRE ME → rebeloper.com/hire-us/
    TOOLS I RECOMMEND:
    → rebeloper.com/tools
    __________
    Get in touch:
    → support@rebeloper.com
    SUBSCRIBE to weekly tips & tutorials for building iOS apps!
    → th-cam.com/users/rebeloper?su...
    GitHub: github.com/rebeloper/
    Hire me: rebeloper.com/hire-us/
    LinkedIn: / rebeloper
    My Blog: rebeloper.com/blog
    Follow me on Instagram: / rebeloper
    ______
    #rebeloper
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 68

  • @rebeloper
    @rebeloper  ปีที่แล้ว +1

    How to use CoreData in SwiftUI 4 👉 th-cam.com/video/UL5muKoGc4k/w-d-xo.html

  • @ShooMPozitiV
    @ShooMPozitiV ปีที่แล้ว

    Just one video about this important thing. Thank you very much!!

    • @rebeloper
      @rebeloper  ปีที่แล้ว

      Glad it was helpful! Share it with someone you think needs it. Next check out 6 State Mistakes Every Junior SwiftUI Developer Makes 👇
      th-cam.com/video/9-2wZs2B13w/w-d-xo.html

  • @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! 👇
      th-cam.com/video/RDtLWWos3cA/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

  • @smwoody88
    @smwoody88 ปีที่แล้ว +6

    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  ปีที่แล้ว +2

      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 👇
      th-cam.com/video/tFA30p1w_vQ/w-d-xo.html

    • @HeroTheOne
      @HeroTheOne ปีที่แล้ว

      Same problem here

    • @dannymeyer24
      @dannymeyer24 5 หลายเดือนก่อน

      @@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 ปีที่แล้ว +1

    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  ปีที่แล้ว

      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.

  • @ZorianaIvaniuk
    @ZorianaIvaniuk ปีที่แล้ว

    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  ปีที่แล้ว

      Agreed! It should have been fixed in iOS16.1. BTW Do you know how to properly name things in code? 👇
      th-cam.com/video/iXO9clXrBj8/w-d-xo.html

  • @thientoan0101
    @thientoan0101 7 หลายเดือนก่อน

    awesome

    • @rebeloper
      @rebeloper  7 หลายเดือนก่อน

      Glad you liked it! Now get ChatGPT 4 for Free in VSCode for iOS and macOS app development 👇
      th-cam.com/video/9pech4iVTWU/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

  • @santhoshVnair
    @santhoshVnair ปีที่แล้ว

    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  ปีที่แล้ว

      Nice suggestion. This seems to be fixed in iOS16 though. BTW check out The BEST Side Hustles For Programmers in 2023 👇
      th-cam.com/video/f6XnN9BJK10/w-d-xo.html

  • @Arthur-eg7uy
    @Arthur-eg7uy ปีที่แล้ว

    Life saver video!

    • @rebeloper
      @rebeloper  ปีที่แล้ว

      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 👇
      th-cam.com/video/gDPYwCKJpeQ/w-d-xo.html

    • @Arthur-eg7uy
      @Arthur-eg7uy ปีที่แล้ว

      @@rebeloper actually no Xcode 14.1 beta 3 still has this issue! I worked on it today!

    • @rebeloper
      @rebeloper  ปีที่แล้ว

      Thanks for letting me know. Use the "hack" provided :) Check out How I CODE For HOURS With NO Breaks 👇
      th-cam.com/video/mWLIrfbsDjM/w-d-xo.html

  • @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  ปีที่แล้ว

      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?

  • @abanoubghaly7685
    @abanoubghaly7685 ปีที่แล้ว

    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  ปีที่แล้ว

      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 👉 th-cam.com/video/-gkvRIVEZss/w-d-xo.html

  • @johnappleseed6926
    @johnappleseed6926 4 หลายเดือนก่อน

    Perfect title

    • @rebeloper
      @rebeloper  4 หลายเดือนก่อน

      Thanks :)
      Next watch: SwiftUI Text - EVERYTHING You Need to Know 👇
      th-cam.com/video/rKEfs5j3ANE/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

  • @falcosolitario
    @falcosolitario ปีที่แล้ว +1

    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  ปีที่แล้ว

      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 👇
      th-cam.com/video/EZz9NL4H0Ug/w-d-xo.html

  • @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 👇
      th-cam.com/video/8O6M7oo5rhk/w-d-xo.html

  • @trevordevs
    @trevordevs ปีที่แล้ว

    To be clear, is this a bug or how we will have to handle bindings from now (iOS 16) and on?

    • @rebeloper
      @rebeloper  ปีที่แล้ว

      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) 👇
      th-cam.com/video/eG603iq6Gpw/w-d-xo.html

  • @martinmajewski
    @martinmajewski ปีที่แล้ว

    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  ปีที่แล้ว

      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 👇
      th-cam.com/video/cMh8ruhTY9k/w-d-xo.html

  • @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: th-cam.com/video/sEycmsJnpvA/w-d-xo.html
      Next check out How I HACKED My Brain To Learn Coding 👇
      th-cam.com/video/2t5KZliLb0Q/w-d-xo.html

    • @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 👇
      th-cam.com/video/aaLRST7tHFQ/w-d-xo.html

    • @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

  • @hunterhillegas4988
    @hunterhillegas4988 ปีที่แล้ว

    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 ปีที่แล้ว

      Looks like it's still an issue in the RC...

    • @rebeloper
      @rebeloper  ปีที่แล้ว

      RC still has this issue :( Next check out 6 State Mistakes Every Junior SwiftUI Developer Makes 👇
      th-cam.com/video/9-2wZs2B13w/w-d-xo.html

  • @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 👇
      th-cam.com/video/DugBVFJ9jRI/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

  • @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! 👇
      th-cam.com/video/3nHK5ctLCNE/w-d-xo.html

  • @anasahalabi2749
    @anasahalabi2749 ปีที่แล้ว

    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  ปีที่แล้ว

      Still there in Xcode14 :( Let's wait and see. BTW here are 6 things I wish I knew about SwiftUI when I started 👇
      th-cam.com/video/EZz9NL4H0Ug/w-d-xo.html

  • @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! 👇
      th-cam.com/video/4149Ru4H4bI/w-d-xo.html

  • @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! 👇
      th-cam.com/video/LUsybFHC4Co/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

  • @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  ปีที่แล้ว +1

      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! 👇
      th-cam.com/video/RDtLWWos3cA/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

  • @chrispugmire
    @chrispugmire หลายเดือนก่อน

    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  วันที่ผ่านมา

      Thanks for the input.
      Have you seen my latest video? 👉 NavigationStack vs. NavigationCoordinator: You Won’t Believe the Difference!
      th-cam.com/video/7fb7CXty5i4/w-d-xo.html

  • @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 👇
      th-cam.com/video/JIgEBLJ6C5A/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper

  • @perhapsyes2493
    @perhapsyes2493 ปีที่แล้ว

    I regret switching our app to SwiftUI.
    So. Fucking. Much.

    • @rebeloper
      @rebeloper  ปีที่แล้ว

      This seems to be fixed in Xcode 14.1 beta 3 :))
      Next: Make Sure You Always Get the Best Error Alerts! 👇
      th-cam.com/video/VWgZ99FTuBU/w-d-xo.html

  • @victorchoi38
    @victorchoi38 9 หลายเดือนก่อน

    I don't like this fix. Apple must fix this.

    • @rebeloper
      @rebeloper  9 หลายเดือนก่อน

      They already did :)
      Next watch Master Keychain Access Like a Pro using SwiftUI Property Wrappers 👇
      th-cam.com/video/B7HBMYEEZtc/w-d-xo.html&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 👇
      th-cam.com/video/0khrcFnzhps/w-d-xo.html&ab_channel=Rebeloper-RebelDeveloper