Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreData Relationship between entities and attributes

I'm having a little trouble grasping CoreData relationships, i'm note sure which relationship type I should be using between my 2 entities or if my logic is correct.

1) "Person" Entity - attributes such as name, tel, address, country, etc...

2) "CountryList" - attributes such as countryName, countryLat, countryLong, etc..

The CountryList entity is pre populated on first run of the app to include all the countries in the world and their respected data.

Where i'm stuck is do I need a relationship between these two entities?

I will be allowing the user to select a country from the CountryList entity data and wish to store there selection in the country attribute for Person entity.

Do I just take the countryName from CountryList as a string and store it in country from Person? or can I make a relationship between them?

I know a user can only belong to 1 country but a country can have lots of users so is this a one to many relationship? Or is it many to many because lots of users can belong to a country but a country can have loads of users? Confused!

Could someone please enlighten me on this and point me in the right direction in what i should be doing in xcode.

Many Thanks in Advance Matt

EDIT: Is this correct?

I have made the changes to Entity names etc and think I now have the relationship set correctly.

Xcode Screenshot

EDIT 2: Removed country attribute and renamed relationships

Xcode Screenshot

like image 776
Matt Price Avatar asked Mar 29 '12 13:03

Matt Price


1 Answers

Firstly, your "CountryList" entity should be called "Country", since it represents only one country. The fact that you have many of those countries has nothing to do with its name.

After that, it seems just natural to use a relationship, one "Person" has one "Country", but one country can have many persons. Therefore, one-to-many relationship. Using a relationship will simplify many operations you might want to perform (i.e. access all the country information of one person, or get a list of all persons being in one particular country).

Oh, and this might help you understand relationships a bit better: There are no "many-to-many" relationships in CoreData per se. You always define a relation from a source to a target. So if you define a relation from Country to Person, this will be a one-to-many relationship. One country, many persons. You can then define a relationship from Person to Country, which would be a one-to-one relationship. One person, one country. If you defined this as an one-to-many relationship, you would end up with a de facto many-to-many relationship (because on person can have many countries and one country can have many persons). It's not as complex as it appears.

Now, after you've defined your two relationships, you can set them as each others "Inverse Relationship". Do it for one of the relationships, the other one will be set automatically. After you did that, CoreData will for example update a Person's country when you add the person to the country's list.

See https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdRelationships.html for further information.

like image 59
JiaYow Avatar answered Sep 30 '22 10:09

JiaYow