如何在核心数据中创建和获取关系记录
现在完全是新手问题......可以说,我已经寻找了一个完全点头的解释,但没有发现任何“愚蠢”的东西。问题是......
我创建了一个核心数据堆栈,其中有一个名为“客户端”的实体和一个名为“汽车”的实体。这是一种一对多的关系。
到目前为止,我已经使用苹果教程中的代码成功创建并获取了客户端列表。一旦我选择了一个客户端,我就会推送一个新的 tableViewController,它应该列出该所选客户端的汽车。
第一个问题...
我习惯于 SQL 风格的数据库编程,如果我想向客户端添加汽车,我只需将“ClientID”标签添加到“汽车”记录中,从而提供与特定客户端的关系。我如何在核心数据中做同样的事情?根据我的阅读,我的理解是添加属性来指向其他实体是不必要的 - 核心数据为您维护这种关系,而不需要在实体中添加其他属性。
第二个问题...
当我创建了一个“汽车”实体并成功将其链接到“客户端”时。如何创建一个仅检索该客户的汽车的提取。我可以更改苹果的代码以获取所有汽车,但我不知道如何获取与给定客户端关联的汽车。从我的阅读来看,我认为我需要使用谓词,但苹果的谓词文档是独立的,并且没有给出关于如何将其与核心数据一起使用的明确指导
我意识到这是多么令人头疼,但我在任何地方都找不到白痴指南......
非常感谢任何帮助/代码示例。
Total newbie question now... Suffice to say, I have searched for a completely noddy explanation but have not found anything 'dumb' enough. The problem is...
I have created a core data stack in which I have a entity called 'Client' and an entity called 'Car'. It is a one-to-many relationship.
So far i have successfully created and fetched the client list using code from apple's tutorial. Once I select a client, I then push a new tableViewController which should list the Cars for that chosen client.
First question...
I am used to sql style database programming where if I wanted to add a car to a client, I would simply add a 'ClientID' tag to the 'Car' record thereby providing the relationship to a specific client. How do I do the equivalent in core data? My understanding from my reading is adding attributes to point to other entities isnt necessary - core data maintains this relationship for you without needing additional attributes in the entities.
Second question...
As and when I have created a 'car' entity and successfully linked it to a 'Client'. How to I create a fetch which will retrieve just THAT client's cars. I could alter the code from apple to fetch ALL cars but I don't know how to fetch cars associated with a given client. From my reading, I think I need to use predicates, but apples predicate documentation stands alone and does not give clear guidance on how to use it with core data
I realise how noddy this is, but I cant find an idiots guide anywhere...
Any help/code exmaples much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我已经回答了我自己的问题。对于那些找到我的问题并想知道答案的人来说,这非常简单......
首先,创建一辆“汽车”并将其与“客户端”相关联。首先像平常一样创建一个“汽车”,然后简单地添加这行代码...
这会将“汽车”记录上的“客户”关系设置为相关客户。
其次,我曾想过,如果您有客户并且需要找到他们的汽车,那么您将需要重新获取。但事实并非如此!只需使用以下代码行...
第一行使用“client.cars”o 遵循对象图来确定该客户端拥有的汽车并将它们填充到 NSSet 中。然后第二行填充一个在当前视图控制器中声明的 NSArray,可用于显示目的。
已排序!
OK, I have answered my own question. For those who have found my question and would like to know the answer, it is extremely simple...
Firstly, to create a 'Car' and associate it with a 'Client'. Firstly create a 'Car' as you normally would and simply add this line of code...
This sets the 'client' relationship on the 'Car' record to the client in question.
Secondly, I had thought that if you had a client and needed to find their cars, you would need a new fetch. But not so! Simply use the following lines of code...
The first line uses "client.cars" o follow the object graph to determine the cars this client has and populates them in an NSSet. The second line then populates a NSArray which is declared in the current viewcontroller which can be used to for display purposes.
Sorted!!