核心数据继承:可以这样做吗? [英] Core Data inheritance: Is it OK to do this?

查看:163
本文介绍了核心数据继承:可以这样做吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:我有一个名为 Car 的实体,它是抽象。然后有两个子实体 Cabriolet Pickup

Example: I have an Entity called Car which is abstract. Then there are two child entities Cabriolet and Pickup.

现在我有一个名为 Driver 的实体,它有一个名为 currentCar 1..1的关系给实体 Car 。所以我可以为任何司机的currentCar属性分配一个敞篷车或皮卡。然后,我需要内省对象,以找出在运行时,如果我有一个敞篷车或皮卡,当我从驱动程序获得currentCar。这是一个有效的设计核心数据?

Now I have an Entity called Driver which has a Relationship called currentCar 1..1 to the Entity Car. So I can assign either a Cabriolet or a Pickup to any driver's currentCar property. Then I would need to introspect the object to find out at runtime if I have a Cabriolet or Pickup when I get the currentCar from the driver. Is this a valid design in Core Data?

推荐答案

我不明白为什么这不工作在技术水平,但它确实违反OOP多态性。

I don't see why this wouldn't work on a technical level, but it does violates OOP polymorphism.

为什么你需要知道汽车的类型?你能够在抽象超类( Car )上定义方法,并在子类( Cabriolet Pickup )?你可以重构car层次结构,使子类的属性变得更通用,并将其移动到 Car 的属性,从而消除了对子类的需要?

Why do you need to know if the type of car? Would you be able to define the methods on the abstract superclass (Car) and override them as appropriate in the the subclasses (Cabriolet and Pickup)? Could you refactor the car hierarchy so that the attributes of the subclasses become more general and move them to attributes of Car, thus removing the need for the subclasses?

在从公共超类中提取对象时,我遇到了 NSFetchResultsController 的问题。 (返回的对象只能通过实体的属性进行排序/分组类类型不是一个属性,所以不能用于排序/分组实体我的解决方案/ hack是一个类型属性到超类 - 丑陋,但它工作。)

I had trouble with NSFetchResultsController when fetching objects derived from a common superclass. (The returned objects can only be sorted/grouped by an attribute of the entity. The class type is not an attribute so cannot be used to sort/group the entities. My solution/hack was to a type attribute to the superclass - ugly, but it worked.)

这篇关于核心数据继承:可以这样做吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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