核心数据关系指向我自己?

发布于 2024-11-16 14:10:37 字数 272 浏览 5 评论 0原文

我需要创建一个与其自身具有一对多关系的核心数据实体(想象一个包含子文件夹的文件夹:我需要访问某个文件夹的所有子文件夹以及任何子文件夹的父文件夹)。

  1. 是否有一种简单的方法可以使用标准核心数据关系来执行此操作,而无需辅助一对多实体?

  2. 我想我可以通过使用自己的关键字段轻松地“以旧方式”完成此操作 - 但是有没有一种简单的方法可以在核心数据中定义自动递增字段? (我可以使用基于时间的值而不是自动递增的值,但出于多种原因我不喜欢这种解决方案)。

I need to create a Core Data entity that has a one-to-many relationship to itself (think of a folder containing subfolders: i need to access all subfolders of a certain folder as well as the parent of any subfolder).

  1. Is there an easy way to do this using standard Core Data relationships, without an auxiliary one-to-many entity?

  2. I guess I can accomplish this easily "the old way" by using my own key field - but is there an easy way to define an auto-incrementing field in Core Data? (I can use a time-based value rather than an auto-incrementing one, but I don't like this solution for several reasons).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风蛊 2024-11-23 14:10:37

核心数据没有字段。不要试图将 Core Data 视为过程化、非对象 API(如 SQL)的术语。你只会让自己感到困惑。

在 Core Data 中创建嵌套关系很简单。要创建一个实体来模拟目录结构,您所需要的只是:

Folder{
  name:string
  parent<<-->Folder.children
  children<-->>Folder.parent
}

任何特定文件夹都有一个父级和多个子级。

不要将数据模型中的实体误认为是活动对象的图形。数据模型中的实体仅定义抽象属性和同样抽象实体的关系,并且可以非常简单。实际的实时图表与其建模的现象一样复杂。

例如,在这种情况下,数据模型将像上面一样简单,一个实体,具有一个属性和两个关系。实时图将像它用数千个独特的Folder对象和数百万个关系建模的文件系统一样复杂。

Core Data doesn't have fields. Don't try to think of Core Data is terms of a procedural, non-object API like SQL. You'll just confuse yourself.

It is trivial to create a nested relationship in Core Data. To make an entity to model a directory structure, all you would need is:

Folder{
  name:string
  parent<<-->Folder.children
  children<-->>Folder.parent
}

Any particular folder has one parent and multiple children.

Don't mistake the entities in the data model for the graph of live objects. The entities in the data model just define abstract attributes and relationships of equally abstract entities and can be very simple. The actual live graph is as complex as the phenomena it models.

E.g. In this case, the data model would be as simple as that above, one entity, with one attribute and two relationships all to itself. The live graph would be as complex as the file system it modeled with thousands of uniqueFolder objects and millions of relationships.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文