NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

SwiftUI dismiss and save model at the same time

Forums > SwiftUI


I used this post to dismiss my SwiftUI view and I used this post to try and get this warning away, that I am modifying the state during a view update.

I am unsure how to do that:

I have a DetailView which takes a Binding<[BonusModel]>. In the detail view I fill in a form and on save, I do bonuses.wrappedValue.append(BonusModel(...)) which adds a bonus to the list of bonuses.

This list is used in MainView as a state, so in this view I am listing all the bonuses. When hitting save on the DetailView, a bonus is added and the bonuses list in the main view is updated. I then want to dismiss the detail view using the first posts' method:


This creates the warning that I am trying to avoid: [SwiftUI] Modifying state during view update, this will cause undefined behavior.

Any idea how I can solve this particular case? Thank you very much.



Did you try to dismiss the dialog asynchronously after the model has been updated? This would allow the model to update the view and only after that, you are dismissing the current view.

DispatchQueue.main.async {


Hacking with Swift is sponsored by NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

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

Not logged in

Log in

Link copied to your pasteboard.