The property is an NSOrderedSet (exerciceHistories).
Piece of Training class :
Code Block extension Training { ... @NSManaged public var date: Date @NSManaged public var exerciceHistories: NSOrderedSet }
The main view is TrainingDetail, displaying detail of Training :
Code Block struct TrainingDetail: View { @Environment(\.managedObjectContext) var managedObjectContext @ObservedObject var training: Training ... var body: some View { ZStack { ... ScrollView(.vertical, showsIndicators: false) { VStack(spacing: 0) { ... TrainingDetailAction(training: training, new: new) //-> View that creates sheet to edit one element of training property NSOrderedSet (`exerciceHistories`) TrainingDetailExercices(training: training, editing: $new) //-> View displaying training property NSOrderedSet (`exerciceHistories`) .padding() }} .... } ... } }
Interesting part of TrainingDetailExercices, where editing sheet is opened :
Code Block struct TrainingDetailExercices: View { @Environment(\.managedObjectContext) var managedObjectContext @ObservedObject var training: Training var body: some View { ... ForEach(training.exerciceHistories.array as! [ExerciceHistory], id: \.self) { exerciceHistory in ExerciceHistoryRow(exerciceHistory: exerciceHistory) .padding(.bottom, 10) }.sheet(isPresented: $showEditView, onDismiss: { ... }, content: { TrainingAddExercice(training: self.training, ...) .environment(\.managedObjectContext, self.managedObjectContext) })
TrainingAddExercice that is the sheet displayed, where we can edit one element of training property NSOrderedSet (exerciceHistories):
Code Block struct TrainingAddExercice: View { ... @ObservedObject private var trainingAddExerciceViewModel: TrainingAddExerciceViewModel @ObservedObject var training: Training ... }
The function that save and close sheet in TrainingAddExercice :
Code Block private func trailingNavButtons() -> some View { HStack{ .... Button(action: { self.trainingAddExerciceViewModel.save(exerciceHistory: self.exerciceHistory, to: self.managedObjectContext, for: self.training) /*here I'm trying to force update by creating a new NSMutableOrderedSet and assign it to training (because NSMutableOrderedSet is class, not struct) */ let newExercicesHistories = NSMutableOrderedSet(orderedSet: self.training.exerciceHistories) self.training.exerciceHistories = newExercicesHistories self.show = false }, label: { ... }).disabled(...) } }
One thing I don't understand is that when I'm adding/deleting one ExerciceHistory to/of training.exerciceHistories, view updates but not when I'm editing one even so I'm trying to force by creating new OrderedSet.
Object is well update in CoreData.
Code Block ForEach(training.exerciceHistories.array as! [ExerciceHistory], id: \.self) { exerciceHistory in ExerciceHistoryRow(exerciceHistory: exerciceHistory) .padding(.bottom, 10) }.id(UUID())