简体   繁体   English

如何设置核心数据实体之间的关系?

[英]How to set the relationship between the core data entities?

So, I have a core data database with multiple relationship already populated with data. 因此,我有一个核心数据数据库,其中多个关系已经填充了数据。 My problem is that I created the relationships between the tables but i don't know how to set them in code. 我的问题是我创建了表之间的关系,但是我不知道如何在代码中设置它们。 I only need to set one and then I can figure it out for the rest of them. 我只需要设置一个,然后我就可以解决其余的问题。 2个核心数据表 For example: Colleges has the "has Groups" relationship because one college has many groups. 例如:由于一所大学有许多小组,所以大学具有“具有小组”关系。 The Groupe table has the "bellongsToColegiu" relationship becouse one or more groups are in one college. Groupe表具有“ bellongsToColegiu”关系,因为一个或多个组在一所大学中。

I know that, i have to set the ".bellongsToCollegiu = Colegiu?" 我知道,我必须设置“ .bellongsToCollegiu = Colegiu?” or ".hasGrupe = Grupe?" 或“ .hasGrupe = Grupe?” but i don't know how to fetch the college in proper way. 但是我不知道如何以适当的方式来读大学。

在此处输入图片说明

For now I have only one college in database. 目前,我在数据库中只有一所大学。

Can someone help me with this please? 有人可以帮我吗? I've been knocking my head for a few days and got nothing. 我已经敲了几天头,什么也没得到。

Thank you and have a nice day! 谢谢你,祝你有美好的一天!

You can fetch a existing 'Colegiu' from the data base or create a new one. 您可以从数据库中获取现有的“ Colegiu”,也可以创建一个新的“ Colegiu”。 for both fetching and creating new one you would need a managed object context. 对于获取和创建新对象,您将需要一个托管对象上下文。 Managed object context is the last part of the data stack which has 3 components comprising of 1. Persistence store, 2. persistence store coordinator, and 3. managed object context. 受管对象上下文是数据堆栈的最后一部分,具有3个组件,包括1.持久性存储,2。持久性存储协调器和3.托管对象上下文。

Here is how to create a stack: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/InitializingtheCoreDataStack.html 以下是创建堆栈的方法: https : //developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/InitializingtheCoreDataStack.html

Then you can create a new object: 然后,您可以创建一个新对象:

let newItem = NSEntityDescription.insertNewObject(forEntityName: 'Colegiu',
                                                          into: 'reference to managed object context here')

or you can search the existing one by calling fetch on managed object context, 或者您可以通过在托管对象上下文上调用fetch来搜索现有的,

Keep in mind CoreData is a complicated topic and it takes time to put the head around. 请记住,CoreData是一个复杂的主题,需要花费很多时间来解决。 All the best. 祝一切顺利。

When you create entities and relationship between them Xcode also generates code for handling those relationships. 当您创建实体及其之间的关系时,Xcode还会生成用于处理这些关系的代码。 The name pattern for code that handles relationships are 处理关系的代码的名称模式是

  1. The same as the name of the relation for to-one relationships 与一对一关系的关系名称相同
  2. addToX and removeFromX (x is the relationship) for to-many relationship. 对多关系的addToX和removeFromX(x是关系)。

So you should have for instance addToHasGrupe(_ value: Grupe) on College and that you can add a group instance to and Grupe should have a belongsToColegiu property that you can set to a college instance. 因此,例如,您应该在College上具有addToHasGrupe(_ value:Grupe),并且可以向其中添加组实例,并且Grupe应该具有可以设置为大学实例的belongsToColegiu属性。 Autocompletion should help you. 自动补全功能可以为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM