简体   繁体   中英

Core data delete all relationship entity

Let say I have core data and three entity inside : Department, Employee, Inventory So every department could have more employees, and every employee could have more items that is record as inventory.

Department <--->> Employee <---->> Inventory

Now lay say, that we have method (service,...) that return new list of employee for department.(Let assume that this could happened quickly.)

The logic is that we delete all instance of Employee of specific Department, and then insert new ones.

The best way would be (for me), that I could do something like that :

 let employees_local = myDepartment.employees
 if let employees = employees_local {
      myDepartment.removeEmployees(employees)
 }

But this (in my understanding) only remove relationship between those object and don't delete all those objects.

I know for solutions where you delete all entity of a kind (delete all Employees) like : this post , or this one .

I even know that I can do a batch delete from ios 9 on. Like here

My question is, is there a faster/ better way to do this. Shouldn't be there a way, that you tell core data to delete all object that have no relationship on parent?

Using the example you looked at above linked as "this one".

If you use a predicate that filters the Employees by checking the relationship to Department is nil, that'd return just the data items you want. Then I suggest you could delete all of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM