UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: onDelete not updating list

Forums > SwiftUI

im doing a litte app using what i'm learning from the 100daysofswiftui i have a navigation view where you can add and delete user an a navigation link to see more details.

in the detail view you can add address to the user and delete it but the delete its not working

@Environment(\.managedObjectContext) var moc
@Environment(\.presentationMode) var presentationMode
@State private var showAddAddressView = false

 let user : User

 ...

 List {
  Section(header: Text("Address")) {
                                ForEach(self.user.addressArray, id: \.self){ address in
                                    AddressCard(address: address)
                                }.onDelete(perform: self.deleteItem)
                            }

                         }
                         .listStyle(GroupedListStyle())
                         .environment(\.horizontalSizeClass, .regular)

...

 func deleteItem(at offsets: IndexSet) {

        for offset in offsets { 
            let address =  self.user.addressArray[offset] 

            self.moc.delete(address)
        }

        try? self.moc.save()

       } 

the problem is this is not updating the list i have to get out of the detailView an then i can see the changes

3      

i had a similar issue and could solve it with marking the object i passed down into my view with @ObservedObject

@ObservedObject var user : User

5      

Thanks to @Volker88 this totally work 🥳

3      

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 free gifts!

Find out more

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

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.