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 :)
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 .
Related
I Have next simple scheme:
Model A
groups ----> Model Group
So model A has one to many relationships to model Group
Group has an inverse relationship to model A
Delete rule - nullify
I have an xml I receive from backed where each model A has an array of Groups
I want to save in Core Data only one entity per Group
So first i check if i have model A entity in Core Data
if i don't - i create it
if i do - i update it with new info from the backend, including an array of Groups
For each Group i also check if it already exists, and then create if it doesn't
Or simple add existing group to model A
And it looks like it's all good - model A has a set of Group entities
But when i fetch the model A entity at another part of the app (another VC) - Group set is empty
And i have no idea why.
Any ideas what is the reason?
Or how i can provide uniqueness of one to many relationship at CD?
looks like correct combo is cascade delete rule + many - to many relationship
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.
I have two tables in coredata Details and person.
Person has two fields Id and Code:
Id Code
IAS RT
IAS TP
IAS IP
Now,detailshas two fields,code and Name
Code Name
RT Rataz
TP Tranzps
IP Irrz
Now I want to use predicates so that i can first fetch the id from person,then after getting the field against the id.I can use the same id as predicate to get Name from person table.How this can be achieved.Any help or suggestion would be appreciated.Thanks in advance.
OK, how you should do this is using a relationship.
Add a relationship to Person called "details" and add a relationship to Detail called "people" (or "person" if it's singular).
Now you can get the person by the id and then use person.details to get the details associated with that person.
Core Data is not a database. Think of it more as a data model. Create the data model and object relationships that you need and use them.
You would be better off checking out a core data tutorial. It will help you in understanding how to use core data.
I am using Core Data in my app and have some entities that have many-to-many relationships. Is there a way to configure the relationships in such way that all entities that don't have at least one entry in some cross-reference table get removed?
Simplified entities:
User:
- songs
- albums
Song
- users
Album
- users
For example, when I remove all users from some album, I want that album to get removed. I know this can be done by checking the number of remaining users, but is there a simpler way?
You can set the delete rule of the relationships in both directions to "Cascade" in the model editor. Core Data should then delete the corresponding entity instances automatically.
You should not follow any advice recommending intermediate "join" tables. This is bad and redundant practice within the Core Data framework and only necessary if you need to store additional information about each relationship itself.
I have a Core data model like the User Entity having relationship with multiple entities. I need to delete all the entities that is related to User entity when i try to remove the user in core data.
How to achieve this?
The easies way is to go to all the relationships in the Core Data model editor, select each and choose the Delete Rule (which is preset to Nullify) to be Cascade.
Now all depending entities will be deleted as well.
Here is the relevant section in the Core Data Programming Guide.
You need to set the delete rule on your relationships. NSCascadeDeleteRule will cascade deletes through the relationships.
You can find the full details on each delete rule in the Documentation. Click on Organizer/Documentation and type in NSDeleteRule for more details.