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

Odd behaviour with SwiftData

Forums > SwiftUI

I have an issue where i am getting different results when using a NavigationLink (works fine) and SwipeAction (does not return correct record, returns last entry in the array) on the same list. Code as below.

Any thoughts, much appreciated.

VStack {
                        List {
                            Section {
                                ForEach(products, id:\.self) { product in
                                    NavigationLink {
                                        EditProductView(product: product)
                                    } label: {

                                        Text(product.name)

                                    }
                                    .swipeActions(edge: .trailing, allowsFullSwipe: false) {
                                        Button(role: .destructive) {
                                            modelContext.delete(product)
                                            try? modelContext.save()
                                        } label: {
                                            Image(systemName: "trash")
                                        }
                                        Button {
                                            editProduct = true
                                        } label: {
                                            Image(systemName: "pencil")
                                                .tint(.myOrange)
                                        }
                                    }
                                    .sheet(isPresented: $editProduct) {
                                        EditProductView(product: product)
                                    }
                                }
                            } footer: {
                                Text("Add product details")
                                    .foregroundColor(.orange)
                            }
                        }
                    }

   

Managed to make this work... not sure if it is orthodox code but it works :)

.swipeActions(edge: .trailing, allowsFullSwipe: false) {
                                        Button(role: .destructive) {
                                            modelContext.delete(product)
                                            try? modelContext.save()
                                        } label: {
                                            Image(systemName: "trash")
                                        }
                                        NavigationLink {
                                            EditProductView(product: product)
                                        } label: {
                                            Image(systemName: "pencil")
                                                .tint(.myOrange)
                                        }
                                    }

   

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

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

You are not logged in

Log in or create account
 

Link copied to your pasteboard.