I have three Entity Objects. The relationship between these entities is kinda love triangle!
Company <<----->> Department (many to many) (eg. HR Department is common to all companies)
Department <---->> Employee
Company <----->> Employee
to set an example,
John (employee1) belongs to BSKYU Company and works in Marketing)
Jason (employee2) belongs to IBC Compnay and works in in Finance)
**Note: Marketing is common to both John and Jason.
I wanted to develop a deletion routine, such that,
deleting Company would delete All its Employees
I also want to get rid of the department that is not part of any `Other Comapny’.
Hence if i delete IBC company, it should delete Jason (via cascade i guess), and also get rid of Finance department ONLY IF IBC is the ONLY COMPANY that had Finance department.
I somehow need to check if Finance.company == [zero result] if so , delete it.
DELETINGDEPARTMENT would delete all its Employees that belong to A Company that was selected before!
Assuming a company was aready selected, I’d and If I delete a deparment , say HR, i’d also want to delete all Employees within that Department THAT BELONGED TO THE SELECTED COMPANY
My question is, “WHERE” should i be handling such deletion rules, this comes across, but im not sure how to handle it:
- (BOOL)validateForDelete:(NSError **)error
What would be the most efficient way to handle the deletion, checking whether everything that needs to be deleted based on the above rules, should be.
any help is appreciated. thanks
You have to do it yourself with the proper predicate, you can use sample code below as a starting point but you’ll have to provide context, predicate, etc.
The delete rules are nice to ensure data integrity but if you have your special cases you have to Fetch the objects and delete them manually.
`
Aside of this, in the datamodel inspector of the relationships you can specify delete rules :
But if you begin to say, i want it to be deleted only if “the employee is one legged, at work, but in the coffee room, except if the big boss is on holidays, then you have to code it.
From the Core Data Programming guide :
source : Core Data Programming Guide