核心数据实体继承 -->限制?

发布于 2024-11-09 18:49:01 字数 587 浏览 2 评论 0原文

我想我会把这个发布到社区。我正在使用 coredata,并且有两个实体。两个实体都具有层级关系。我现在注意到有很多重复的功能,并且想知道是否应该重新构建一个抽象的基本实体(HierarchicalObject),并让我的实体继承它们。

所以问题是我应该考虑这种继承的一些限制吗?阅读了一些帖子,我看到了一些权衡,请告诉我我的假设是否正确。

  1. (好)清理结构,将 HierarchicalObject 功能保留在一处。
  2. (好的)通过继承,两个对象现在最终都在同一个 sqlite 表中(我使用 Sqlite 作为后端)。那么如果对象数量增加,搜索/排序可能需要更长的时间?不确定这是否是一个大问题,因为我的例子中的对象数量应该保持相当静态。
  3. (不太好)有了继承,关系会变得更复杂吗? (http://www.cocoadev.com/index.pl?CoreDataInheritanceIssues)

有吗还有其他需要考虑的事情吗?

感谢您的评论。

I thought I'll post this to the community. I am using coredata, and have two entities. Both entities have a hierarchical relationship. I am noticing quite a lot of duplicated functionality now, and am wondering if I should re-structure to have a base Entity which is abstract (HierarchicalObject), and make my entities inherit from them.

So the question is are there some limitations of this inheritance that I should take into account? Reading some of the posts out there, I see a few trade-offs, let me know if my assumptions are correct.

  1. (Good) clean up structure, keep the HierarchicalObject functionality in one spot.
  2. (Ok) With inheritance, both objects now end up in the same sqlite table (I am using Sqlite as the backend). So if the number of objects grow, search/sorting could take longer? Not sure if this is a huge deal, as the number of objects in my case should stay pretty static.
  3. (not so good) With inheritance, the relationship could get more complicated? (http://www.cocoadev.com/index.pl?CoreDataInheritanceIssues)

Are there other things to take into account?

Thanks for your comments.

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

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

发布评论

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

评论(3

披肩女神 2024-11-16 18:49:01

我认为将实体和类进行比较是错误的。虽然非常相似,但它们确实存在一些重要的差异。

最重要的区别是实体没有像类那样的代码,因此当您拥有具有重复属性的实体时,您不会添加大量额外的编码和引入错误的可能性。

很多人认为类继承必须与实体继承并行。事实并非如此。只要一个类从 NSManagedObject 派生并响应它所代表的实体的正确键值消息,该类就可以在其继承中拥有许多快乐的冒险,而这些冒险并没有反映在实体继承中。例如,在 NSManagedObject 下面创建一个自定义基类是相当常见的,并且所有后续的托管对象子类都继承自该基类,而不管它们的实体如何。

我认为唯一绝对需要实体继承的时候是当您需要不同的实体出现在同一关系中时。例如:

Owner{
  vehical<-->Vehical.owner
}

Vehical(abstract){
  owner<-->Owner.vehical
}

Motocycle:Vehical{ 

}

Car:Vehical{

}

现在 Owner.vehical 可以容纳 Motocycle 对象或 Car 对象。请注意,MotocycleCar 的托管对象类继承不必相同。您可以拥有诸如 Motocycle:TwoWheeled:NSManagedObjectCar:FourWheeled:NSManagedObject 之类的东西,一切都会正常工作。

最后,实体只是上下文的指令,告诉它对象图如何组合在一起。只要您的实体安排能够实现这一点,您在设计细节上就有很大的灵活性,比类的类似情况下的灵活性要大得多。

I think it's a mistake to draw to close a parallel between entities and classes. While very similar they do have some important differences.

The most important difference is that entities don't have code like a class would so when you have entities with duplicate attributes, your not adding a lot of extra coding and potential for introducing bugs.

A lot of people believe that class inheritance must parallel entity inheritance. It does not. As a long as a class descends from NSManagedObject and responds to the right key-value messages for the entity it represents, the class can have many merry adventures in it's inheritance that are not reflected in the entities inheritance. E.g. It's fairly common to create a custom base class right below NSManagedObject and the have all the subsequent managed object subclasses inherit from that regardless of their entities.

I think the only time that entity inheritance is absolutely required is when you need different entities to show up in the same relationship. E.g:

Owner{
  vehical<-->Vehical.owner
}

Vehical(abstract){
  owner<-->Owner.vehical
}

Motocycle:Vehical{ 

}

Car:Vehical{

}

Now the Owner.vehical can hold either a Motocycle object or a Car object. Note that the managed object class inheritance for Motocycle and Car don't have to be same. You could have something like Motocycle:TwoWheeled:NSManagedObject and Car:FourWheeled:NSManagedObject and everything would work fine.

In the end, entities are just instructions to context to tell it how the object graph fits together. As long as your entity arrangement makes that happen, you have a lot flexibility in the design details, quite a bit more than you would have in an analogous situation with classes.

小伙你站住 2024-11-16 18:49:01

我认为提及 iOS 10 上的 Notes 应用程序在其核心数据模型中使用继承会很有用。他们使用一个基本实体SyncingObject,该实体有7个子实体,包括Note和Folder。正如您所提到的,所有这些都存储在同一个 SQLite 表中,该表有多达 106 列,并且由于在所有实体之间共享,所以大多数都是 NULL。他们还将文件夹注释一对多关系实现为多对多关系,从而创建数据透视表,这可能是解决继承问题的方法。

使用实体继承有几个优点,可能会超越这些存储限制。例如,唯一约束在实体之间可以是唯一的。对父实体的提取请求可以返回多个子实体,从而使使用提取结果控制器的 UI 更加简单,例如按侧边栏中的帐户或文件夹进行分组。 Notes 使用此在文件夹行上方显示“所有注释”行,该行实际上是帐户子实体。

I thought it would be useful to mention that the Notes app on iOS 10 uses inheritance in its Core Data model. They use a base entity SyncingObject, that has 7 sub-entities including Note and Folder. And as you mentioned all of these are stored in the same SQLite table which has a whopping 106 columns, and since are shared among all entities most are NULL. They also implemented the folder-notes one-to-many relation as a many-to-many which creates a pivot table, which might be a work-around for an inheritance problem.

There are a couple of advantages to using entity inheritance that likely outweigh these storage limitations. For example, a unique constraint can be unique across entities. And a fetch request for a parent entity can return multiple child entities making UI that uses fetched results controller simpler, e.g. grouping by accounts or folders in a sidebar. Notes uses this to show an "All Notes" row above the Folder rows which is actually an Account child entity.

客…行舟 2024-11-16 18:49:01

我过去在继承模型的数据迁移方面遇到过问题 - 您可能想尝试一下,看看是否可以让它工作。

正如您还指出的那样,所有对象都放在一张表中。

然而,由于 Core Data 正在管理对象图,因此以自然方式建模对象(包括继承)的方式保持结构确实很好。为了保持模型的健全,有很多话要说,这样您就可以减少维护代码的工作。

我个人在我自己的一个应用程序中使用了一个相当复杂的带有继承的 CD 模型,并且效果很好(除了我所说的数据迁移问题,但这对我来说一般来说太不稳定了,我不依赖不再继续工作)。

I have had issues in the past with data migration of models that had inheritance - you may want to experiment with that and see if you can get it to work.

As you noted also, all objects go in one table.

However, as Core Data is managing an object graph, it is really nice to keep the structure the way you would naturally have it just modeling objects - which includes inheritance. There's a lot to be said for keeping the model sane so that you have to do less work in maintaining code.

I have personally used a fairly complex CD model with inheritance in one of my own apps, and it has worked out OK (apart from as I said having issues with data migration, but that has been so flakey for me in general I do not rely on that working any longer).

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