Reputation: 759
There two entities Parent
and Child
, which is one to many relationship. One Parent and many Child.
I use EditMode
to delete Child
data like:
@ObservedObject private var db = CoreDataDB<Child>(predicate: "parent")
var body: some View {
VStack {
Form {
Section(header: Text("Title")) {
ForEach(db.loadDB(relatedTo: parent)) { child in
if self.editMode == .active {
ChildListCell(name: child.name, order: child.order)
} else {
...
}
}
.onDelete(perform: self.db.deleting) <--- How to update CoreData when delete occurs in this line
}
}
}
}
The deleting
method is defined in another Class like:
public func deleting(at indexset: IndexSet) {
CoreData.executeBlockAndCommit {
for index in indexset {
CoreData.stack.context.delete(self.fetchedObjects[index])
}
}
}
And I also want to update other Attribute of Parent
and Child
Entities when onDelete
occurs. How to make it?
Thanks for any help.
Upvotes: 1
Views: 45
Reputation: 257663
Here is possible approach
Section(header: Text("Title")) {
ForEach(db.loadDB(relatedTo: parent)) { child in
if self.editMode == .active {
ChildListCell(name: child.name, order: child.order)
} else {
...
}
}
.onDelete { indices in
// make other update here ...
self.db.deleting(at: indices)
// ... or here
}
}
Upvotes: 1