0
votes

I implemented core data in my app with 2 Entity named "Employees" & "Departments".

They have a relationship with many to many. But no action for deletion now on relationship.

Now, I need like if any department has no employee then that department will automatically delete from DB.

Is this possible?

Thanks for your time :)

1
You can't do this automatically. You will need to write code to look for departments with no employees and delete them - Paulw11
@Paulw11, thanks a lot for your answer! Can you please help me to get departments which has no employee & delete them. - user2526811
Since you have many to many relation , when you delete employee NSManagedObject, you can check if departments array is one (that means current employee is the only object in that department) you can delete obtained department object too. - Bhumit Mehta
@BhumitMehta, thanks a lot for your answer actually I am not deleting employees. Just change the department of employee. So I Need to delete all departments if there is no employee for it. - user2526811
You can do something similar, when you assign employee to new department ManagedObectContext you can retrive old department object and do the check as in previous comment. You will have to write some logic which suits your scenarios , as others have said there is nothing that will automatically delete the department object in this case - Bhumit Mehta

1 Answers

0
votes

It is not possible to delete automatically if we have a trouble to fetch u need to filter the data using predicate matching with your requirements .