Core Data Relationship issue - ios

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

Related

Delete objects form Core Data

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 .

Creation of relationship with attribute in CoreData with iOS in Objective C

I would like to create a model with two entities Orders and Products. They are linked by a relationship 'Contain' that has an attribute 'quantity'. How can I represent that in CoreData ? (Do not send me the ray tutorial, or any tutorial on youtube, I think I have done every thing). It is very important the relationship with attribute and not something general. (I know that it is not a database, but it is a Conceptual data model/Conceptual Schema as it is named by "entities" and "relationship" so if there is relationships, there must be a way to have relationships with attribute).
EDIT :
Am I doing the right thing by not adding id_order and id_product to the Contain entity ?
In CoreData, Contain would be another entity. It would have relationships to Orders and Products and a quantity attribute.
You cannot add attributes to a relationship in CoreData.

Core data - remove entity if no foreign keys

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.

Core Data - Relations queries

In core data,
I want to use relationship. But I have a doubt in my mind. Apologies but I didn't find clear answers on various websites and blogs. Other things like Add, Fetch, Delete queries are clear to me. But I have some questions in core data which are still not clear.
I have made two Entities:
Photographer [Attributes : name and camera]
Photo [Attributes: zoner and photographerName] .
1) I have connected them with relations. So if I connect this two entities with relations then I should remove that photographerName from Photo?
2) As I have connected these two entities with the relation then how can I use the photographer name with the photo Entity?
3) How can I add values in this if I use it with relations? [Now it is showing me Null in the relation from sqlite browser]
So if I connect this two entities with relations then I should remove that PhotographerName from Photo ?
Generally, yes, it is redundant.
how can I use the photographer name with the photo Entity ?
self.photographer.name
(assuming you are in the Photo class and the relationship names is photographer)
How can I add values in this if I use it with relations
I guess you mean how can I set the relationship value. Create an instance (or find an existing instance) or each entity and then:
photo.photographer = photographer;
Notes:
Ensure that the relationship has an inverse
Names the relationship ends photographer and photos (1 to many)
Try to set the photographer of a photo, or use the relationship methods auto-generated in the Photographer class to add photos

Core Data Model

I'm struggling with creating a suitable Core Data model for my app. I'm hoping someone here can provide some guidance.
I have two entities -- "Goals" and "Items". The Goals entity contains only a goal description, but any goal may have any number of subgoals, and these may extend multiple levels in a tree structure. Subgoals are to be contained within the same entity, so presumably the Goal entity will contain a pointer to "parent" which will be the parent goal of any subgoal.
There will also be an "Items" entity that contains a couple of text fields and a couple of binary items, and must be linked (ideally, by a unique identifier, perhaps objectID) to the particular goal or subgoal the item(s) are related to.
I am totally fumbling with how to set this model up. I know what attributes need to be in each entity, but the relationships, particularly between goals and "subgoals", has me stumped. I don't seem to be able to turn up any good examples of tree structures in Core Data on the Internet, and even the couple of books I have on Core Data don't seem to address it.
Can anyone here help an old SQL programmer get headed the right direction with these relationships in Core Data? Thanks.
Have you tried creating a one-to-many from Goal to itself, and a one-to-one from Goal to Item? The only thing I would worry about here is circular references.
Also, read Relationships and Fetched Properties in the CoreData Programming Guide.
Here is how it is done:
You set up a to-many relationship from Goal to Item in the model editor. Don't use any ids, foreign keys etc. This is old-fashioned database thinking - you can forget about it. Here we are only dealing with an object graph. The database layer is just an implementation detail for persisting the data.
Make two more relationships in entity Goal to itself: a to-one called parent, a to-many called subGoals. Make them the inverse of each other. Simple!
QED is correct, you can create a to many relationship on goal (call it subgoals) as well as a to-one relationship on goal (call it parentGoal) and set them as inverses to each other.
Then create another to many relationship (call it items) on the goal entity, with the inverse being a to one relationship on the item entity (call it goal). Then you're all set. You don't need to link items with a unique id, just add them to the items relationship.
Also note that if you did want to give items a unique id, do not use the objectID. The objectID should only be used as a temporary id as they are not guaranteed to remain the same. In fact they will change if you ever do a Core Data migration.
One way, though not really great, is to create a another entity, say subGoal, and each goal has one subGoal and each object of subGoal has many goal.

Resources