Nesting Issue In Core Data - ios

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.

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.

To-Many relationship (CoreData) with counters

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.

Core Data Relationships on Abstract Entities

Is it legitimate to create a one to one relationship between two entities when one is set to be abstract ?
An abstract entity is not meant to be instantiated. That's why you cannot create such a relationship. What you could do though is to create a relationship where the entity(s) are inheriting from an abstract entity.
From Apple's docs:
A relationship specifies the entity, or the parent entity, of the
objects at the destination. This can be the same as the entity at the
source (a reflexive relationship). Relationships do not have to be
homogeneous. If the Employee entity has two sub-entities, say Manager
and Flunky, then a given department's employees may be made up of
Employees (assuming Employee is not an abstract entity), Managers,
Flunkies, or any combination thereof.
EDIT:
Apparently you could create such a relationship (so that child entities would inherit the relationship as well)...
If you define an entity inheritance hierarchy (see “Entity
Inheritance”), when you specify a super-entity as the entity for a
fetch request, the request returns all matching instances of the
super-entity and of sub-entities. In some applications, you might
specify a super-entity as being abstract (see “Abstract Entities”). To
fetch matching instances of all concrete sub-entities of the abstract
entity, you set the entity for fetch specification to be the abstract
entity. In the case of the domain described in “Abstract Entities,” if
you specify a fetch request with the Graphic entity, the fetch returns
matching instances of Circle, TextArea, and Line.
See also this answer: Core Data: Abstract Entity in Fetch Request
yes. you can have a person who owns a "thing"...

Resources