本文介绍了如何使用核心数据创建递归一对多关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前使用过核心数据,但是我必须构建的该数据库具有特殊性,这将需要一种特殊的关系来融化我的大脑.

I have used core data before, but this database I have to construct has a particularity that will require a special relationship that is melting my brain.

我有2个实体,我们称它们为笼子"和动物".

I have 2 entities, lets call them Cage and Animal.

  • 笼子有一个属性name
  • 动物具有nameimage属性,并且必须跟踪其子代.
  • Cage has one attribute name
  • Animal has a name and image attributes and must keep track of its children.

可能的结构可能类似于

cage ---------- animal 1
         |
         |_____ animal 2
         |
         |_____ animal 3 ____ animal 4
                          |
                          |__ animal 5
                                  |
                                  |_____ animal 6

查看此结构,您会发现Animal 1Animal 2Animal 3具有Cage作为父级,或者如果愿意的话,它们是Cage的子级"对象.另一方面,Animal 3有2个子对象(4和5),而Animal 5有一个子对象.

Looking at this structure you see that Animal 1, Animal 2 and Animal 3 have Cage as parent or are "children" objects of Cage if you will. On the other hand Animal 3 has 2 children objects (4 and 5) and Animal 5 has one children object.

我需要CageAnimal是不同的实体.

I need Cage and Animal to be different entities.

因此,您看到Animal对象可以将其他动物作为子级.一种动物可以有一个父母,但可以有多个孩子.一个笼子对象只能有子代.

So, you see that Animal objects can have other Animals as children. An animal can have one parent but multiple children. A Cage object can only have children.

我试图添加一个children关系,目标等于Animal,反之等于children(toMany),但这正是融化我的大脑的原因,因为如果我这样做,它将一次成为父母和孩子在其他地方,更不用说孩子这个名字会使编码难以将头缠起来...

I have tried to add a children relationship with destination equal to Animal and inverse equal to children (toMany) but this is what is melting my brain, because if I do this, this will be parent at one time and children at others, not to mention that the name children will make coding difficult to wrap the head around...

要使这项工作有效,我必须如何建立实体之间的关系?

How do I have to build the relationships between the entities to make this work?

推荐答案

首先创建您的实体和属性.然后建立您的关系. 动物"和孩子"的关系被设置为很多".将笼"和父母"设置为一对一"关系.最后,为每个关系设置目标和倒数.

First create your entities and attributes. Then create your relationships. The relationships for "animals" and "children" shout be set to "to many". Leave "cage" and "parent" set to "to one" relationship. Finally, set the destination and inverse for each relation.

动物"和笼子"之间的关系应该成反比.关系父母"和孩子"应设置为相反.

Relations "animals" and "cage" should be inverse.Relations "parent" and "children" should be set to inverse.

完成后应该像这样:

这篇关于如何使用核心数据创建递归一对多关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:03