Edit a list item (using CoreData)

Hi together,

I'm currently building an app, where I have a List, in which each item links to a detail view using NavigationLink. The data is stored using CoreData. So far, everything works.

I now would like to modify the data in the DetailView. For simplicity, I first wrote the following code, that uses an store-object. This does work.

MainView:

@ObservedObject var birthdayStore = BirthdayStore()
// ...
List{
    ForEach(filteredBirthdays){ birthday in
        NavigationLink(destination: BirthdayDetail(birthday: self.$birthdayStore.birthdays[self.birthdayStore.birthdays.firstIndex(of: birthday)!])){
            // Display item
        }
    }
}

Detail-View:

struct BirthdayDetail: View {
    @Binding var birthday: Birthday
    
    var body: some View {
        Form{
            TextField("Name", text: $birthday.name)
        }
        .navigationTitle(birthday.name)
    }
}

I now tried to achieve the same thing using core data, but struggle to get it to work.
In the Main-View, I now fetch my data like so:

@Environment(\.managedObjectContext) private var moc
    
@FetchRequest(entity: Birthday.entity(), sortDescriptors: [])
private var birthdays: FetchedResults<Birthday>

Would be great, if someone could help me with this! :)

Terms of Service

Privacy Policy

Cookie Policy