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      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.