To-Many relationship (CoreData) with counters - ios

I have a project with two entities - Ingredients and Dishes, with many to many relationships.
In every dish I keep a relationship to the ingredients. I am trying to have a counter for the right quantity for each ingredient for every dish (for example, dish "Omlet" should have "Egg" with property "2" and "Milk" with property "1"....).
I am struggling with the right way to model the counter and how to define it, any help there?

You need a third entity to model this. First, remove your to-many relationship. Then create a new entity called IngredientQuantity (or whatever you like) and add a property amount of type integer. Now, add two to-one relationships to IngredientQuantity. The first one points to your Dish entity and the second one to your Ingredient entity.

Related

CoreData Parent Children one to many inverse relationships using single entity

I have a problem how to model properly in CoreData a Person with it's relationship to children with inverse using single entity to model.
I want to model a family, so I have created an entity Person for each member. A Person can have parents, a father and mother - for this I have created one-to-one relationship. Person can have children which is a set of one-to-many relationship.
I Have a problem with setting up the inverse in CoreData model as it is required for the relationships. The problem is that when I set father/mother attribute of entity Person to children it can only be set for only one of them, not for both a father and mother. Do you have a solution how to model this properly?
There's multiple ways you can go about this.
Personally, I would just have a parents and a children property and leave it at that.
If you really want to differentiate between mother and father, you could use a single parents/children relationship and look for the Person with the required gender.
Another alternative is to create a ParentRelationship object that points to a parent and a child and describes the relationship.

Adding properties to many to many relationship?

I have been using Core Data to model my database. I have 2 entities that are in many to many relationship. Each person can have many addresses, and on each address multiple persons can live.
Now i would like to add property to this relationship. For example one person - address will have label home, other person - address can will have label mama's place.
I can't add this property on address entity, because same address will have different labels for different persons.
Since relationships are modeled like NSSets, I don't see a way to do what I want.
Can this be somehow done?
It is not possible to add attributes to a many-many relationship directly. The Apple-recommended approach (see "Modelling a relationship based on its semantics" in the CoreData Programming Guide) is to replace the many-many relationship with an intermediate entity, to which you add the attributes. Each of your existing entities will have a one-many relationship with the new entity.
In your case, you might have something like this:
Person <--->> PersonAddressDetails <<---> Address
You can then add the label attribute to the PersonAddressDetails entity.

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.

Multiple entries in a many to many core data model

I have a core data model with ShoppingList and Product entities joined with a many to many relationship. For some reason its not letting me add a Product more than once to a ShoppingList. What do I need to do to have the same product exist multiple times in a ShoppingList?
Any guidance would be highly appreciated.
In a many-many relationship, any one Product can appear on many ShoppingLists, and any one ShoppingList can have many Products on it. But, as you have found, any one product can appear on any one shopping list only once. (The relationships are represented in the entity definitions as NSSet, which are unordered and cannot have duplicates.)
If you want to record some kind of quantity associated with that product on that shopping list, you need to do things slightly differently. Rather than have a many-many relationship directly from Product to ShoppingList, create a separate, intermediate entity, call it something like Quantity, and give it an attribute to hold the quantity details. Then create one-many relationships from Product to Quantity and from ShoppingList to Quantity.

Nesting Issue In Core Data

I have created a database in which I have to store contacts in various categories. The issue comes when I have to create Sub categories in a Category like :-
Categories ->
Sub Categories->
Contacts
But the Categories can also have Contacts like
Categories -> Contacts
where the sub categories can also have contacts. I figured that nesting in core data would be used. How can I achieve this kind of a relationship ? How do I save the sub categories into the categories even though they are of the same entity ?
I have attached my core data entity relationship model here :-
There is no problem creating a "self" referencing relationship in CoreDate.
In other words, An entity may have a relationship of its own kind.
The only difference in your case between a Category and a SubCategory is the existence of a parent entity.
So there is no need to define a new entity for that part.
You can simply define a relationship:
Category.parent of type Category (say to-one in this case)
and a reverse relationship of:
Category.subCategories of type NSSet (to-many in this case) containing Category objects.
You can set all that up in your interface builder.
Now, since Category has a relationship with Contact so does all the "sub-categories" will have that relationship.
If you like your "sub-categories" to have additional properties, simply create a new entity an make it inherit from your Category entity (keeping the above setting I described).
and add to it the new properties.

Resources