I'm trying to update some label texts for my contexMenu depending on the value stored for that object on Core Data. Just before, on the Button definition, I change and save the reference value:
.contextMenu {
Button {
student.marked.toggle()
student.setValue(student.marked, forKey: "marked")
do {
try moc.save()
} catch {
print(error.localizedDescription)
}
} label: {
if student.marked {
Label("Unmark", systemImage: "bookmark")
} else {
Label("Mark", systemImage: "bookmark.fill")
}
}
}
Data is saved correctly on Core Data, as the rest of the information displayed on view changes in real time. But label text doesn't, until I kill the app and open it again, when both image and text are changed according to the reference value.
I have my Core Data code just at the begining of the view, so everything must update instantly:
@Environment(\.managedObjectContext) var moc
@FetchRequest(sortDescriptors: [SortDescriptor(\.name)]) var students: FetchedResults<Student>
What could I have been doing wrong?
Thanks!