我正在尝试更新 NSManagedObject ( Training
) 的属性更新的 SwiftUI (子) 视图。该属性是一个 NSOrderedSet ( exerciceHistories
)。
类Training
:
extension Training {
...
@NSManaged public var date: Date
@NSManaged public var exerciceHistories: NSOrderedSet
}
主视图是TrainingDetail
,显示详细信息Training
:
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()
}}
....
}
...
}
}
有趣的部分TrainingDetailExercices
,编辑表打开的地方:
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 是显示的表格,我们可以在其中编辑训练属性 NSOrderedSet ( exerciceHistories
) 的一个元素:
struct TrainingAddExercice: View {
...
@ObservedObject private var trainingAddExerciceViewModel: TrainingAddExerciceViewModel
@ObservedObject var training: Training
...
}
保存和关闭工作表的功能TrainingAddExercice
:
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(...)
}
}
我不明白的一件事是,当我向 training.exerciceHistories 添加/删除一个 ExerciceHistory 时,查看更新,但在我编辑一个时却没有,所以我试图通过创建新的 OrderedSet 来强制执行。
对象在 CoreData 中得到了很好的更新。