I have four models: Subject
> Topic
> Content
> Block
.
When deleting a Subject
I also want to delete the nested Topic
, Content
and Block
.
When deleting a Topic
I want to delete the nested Content
and Block
.
When deleting a Content
I want to delete the nested Block
.
How should I do that?
I tried to put this in the comments, but its too small to see. I don't recommend forcing CoreData I think the problem may be somewhere else and I would definitely give it some thought. But in a pinch you can just delete the references.
I think this code is right:
func delete(_ item: NSManagedObject) {
let context = item.managedObjectContext!
switch item.entity.name {
case .some("Subject"):
(item as! Subject).topic.forEach({delete($0)})
case .some("Topic"):
(item as! Topic).content.forEach({delete($0)})
case .some("Content"):
(item as! Content).block.forEach({delete($0)})
default:
break
}
context.delete(item)
}
I don't have a quick way to test right now. But something recursive like this would probably be your best bet.