xcode CoreData 实体和属性之间的关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9926380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:49:46  来源:igfitidea点击:

CoreData Relationship between entities and attributes

iphoneobjective-ciosxcodecore-data

提问by Matt Price

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.

我在掌握 CoreData 关系时遇到了一些麻烦,我确定我应该在我的 2 个实体之间使用哪种关系类型,或者我的逻辑是否正确。

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

1)“人”实体 - 属性,如姓名、电话、地址、国家等...

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

2)“CountryList”——countryName、countryLat、countryLong等属性。

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

CountryList 实体在应用程序第一次运行时预先填充,以包括世界上所有国家及其受尊重的数据。

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.

我将允许用户从 CountryList 实体数据中选择一个国家,并希望将选择存储在 Person 实体的 country 属性中。

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?

我是否只是将 CountryList 中的 countryName 作为字符串并将其存储在 Person 中的 country 中?或者我可以在他们之间建立关系吗?

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.

有人可以就此启发我,并指出我应该在 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

Xcode 截图

EDIT 2: Removed country attribute and renamed relationships

编辑 2:删除国家/地区属性并重命名关系

Xcode Screenshot

Xcode 截图

采纳答案by JiaYow

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.

首先,您的“CountryList”实体应该被称为“Country”,因为它只代表一个国家。事实上,你拥有许多这样的国家与它的名字无关。

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.

哦,这可能会帮助您更好地理解关系:CoreData 本身没有“多对多”关系。您始终定义从源到目标的关系。因此,如果您定义从 Country 到 Person 的关系,这将是一对多关系。一个国家,很多人。然后,您可以定义从 Person 到 Country 的关系,这将是一对一的关系。一个人,一个国家。如果您将其定义为一对多关系,您最终会得到事实上的多对多关系(因为一个人可以有多个国家,一个国家可以有多个人)。它并不像看起来那么复杂。

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.

现在,在定义了两个关系后,您可以将它们设置为彼此的“反向关系”。为其中一个关系执行此操作,另一个关系将自动设置。执行此操作后,CoreData 将例如在您将人员添加到国家/地区列表时更新该人员的国家/地区。

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

有关更多信息,请参阅https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdRelationships.html

回答by trapper

CountryListshould just be Country

CountryList应该只是 Country

Then you set a 'many to one' relationship between Person.county and Country

然后在 Person.county 和 Country 之间设置“多对一”关系

回答by rohan-patel

You are using Core Data so you must define relationship between Personand Countryif you want to fetch person's country from database.

您正在使用核心数据,因此您必须定义之间的关系Person以及Country是否要从数据库中获取人员的国家/地区。

And in this relationship you may take one to one relationship. As One person will belong to one country only. Of Course a country will have many person but unless you want to show all people related to particular country you do not need one to many relationship..

在这种关系中,您可能会采取一对一的关系。因为一个人只属于一个国家。当然一个国家会有很多人,但除非你想展示所有与特定国家相关的人,否则你不需要一对多的关系。

In case you want to implement one to many relationship thistutorial link maybe helpful to you for understanding one to many relationship..

如果您想实现一对多关系,教程链接可能对您理解一对多关系有所帮助。

http://www.theappcodeblog.com/2011/09/29/iphone-app-development-tutorial-core-data-part-2-one-to-many-relationship/

http://www.theappcodeblog.com/2011/09/29/iphone-app-development-tutorial-core-data-part-2-one-to-many-relationship/