核心数据实体继承 - >限制? [英] Core Data entity inheritance --> limitations?

查看:136
本文介绍了核心数据实体继承 - >限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. (Ok)通过继承,两个对象现在都位于同一个sqlite表中(我使用Sqlite作为后端)。因此,如果对象的数量增长,搜索/排序可能需要更长时间?不确定这是否是一个巨大的交易,因为我的情况下对象的数量应该保持漂亮的静态。

  2. (不是那么好)继承,关系可能会更复杂? ( http://www.cocoadev.com/index.pl?CoreDataInheritanceIssues ) li>
  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?

感谢您的意见。

推荐答案

我认为绘制一个错误来关闭实体和类之间的并行。虽然非常相似,他们有一些重要的区别。

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.

很多人认为类继承必须并行实体继承。它不是。只要一个类从NSManagedObject继承,并且响应它表示的实体的正确的键值消息,该类可以在它的继承中具有许多不反映在实体继承中的快乐冒险。例如。在NSManagedObject之下创建一个自定义的基类是相当普遍的,并且所有后续的托管对象子类继承自它,而不管它们的实体。

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{

}

Owner.vehical可以持有 Motocycle 对象或 Car 对象。请注意, Motocycle Car 的托管对象类继承不必相同。你可以有 Motocycle:TwoWheeled:NSManagedObject Car:FourWheeled:NSManagedObject ,一切都会正常。

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.

这篇关于核心数据实体继承 - &gt;限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆