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

Delete from CoreData

Forums > Videos

I just follow this Paul video about CoreData and i'm trying to implement the deletion of an element using .onDelete explained in this article. The problem is no matter which row i swipe, this action always remove the first element in the list . Can anyone help me?

import SwiftUI

struct ContentView: View {
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(entity: Country.entity(), sortDescriptors: []) var countries: FetchedResults<Country>

    var body: some View {
            VStack {
                List {
                    ForEach(countries, id: \.self) { country in
                        Section(header: Text(country.wrappedFullName)) {
                            ForEach(country.candyArray, id: \.self) { candy in
                                Text(candy.wrappedName)
                            }
                        }
                    }
                    .onDelete(perform: removeCountry)
                }

                Button("Add") {
                    let candy1 = Candy(context: self.moc)
                    candy1.name = "Mars"
                    candy1.origin = Country(context: self.moc)
                    candy1.origin?.shortName = "UK"
                    candy1.origin?.fullName = "United Kingdom"

                    let candy2 = Candy(context: self.moc)
                    candy2.name = "KitKat"
                    candy2.origin = Country(context: self.moc)
                    candy2.origin?.shortName = "UK"
                    candy2.origin?.fullName = "United Kingdom"

                    let candy3 = Candy(context: self.moc)
                    candy3.name = "Twix"
                    candy3.origin = Country(context: self.moc)
                    candy3.origin?.shortName = "UK"
                    candy3.origin?.fullName = "United Kingdom"

                    let candy4 = Candy(context: self.moc)
                    candy4.name = "Toblerone"
                    candy4.origin = Country(context: self.moc)
                    candy4.origin?.shortName = "CH"
                    candy4.origin?.fullName = "Switzerland"

                    try? self.moc.save()
                }
        }
    }

    func removeCountry(at offSets: IndexSet) {
        for index in offSets {
            let country = countries[index]
            moc.delete(country)
        }
    }
}

5      

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.

Learn more here

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.