Core Data 中的抽象实体和继承 [英] Abstract entities and inheritance in Core Data

查看:24
本文介绍了Core Data 中的抽象实体和继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 3 个实体的一级方程式比赛数据模型:

I have a data model for Formula 1 races with 3 entities:

  • RacingActor:抽象实体
  • Pilot:继承自 RacingActor
  • 团队:继承自 RacingActor

如果我生成 NSManagedObject 子类来表示这些实体,则生成的代码根本不代表这种设计:

If I generate NSManagedObject subclasses to represent these entities, the code generated doesn't represent at all this design:

  • 一切都继承自 NSManagedObject
  • 没有什么能阻止我实例化 RacingActor
  • Pilot 中的 team 属性属于 NSManagedObject 类型,而不是 Team
  • Everything inherits from NSManagedObject
  • Nothing prevents me from instantiating RacingActor
  • The team property in Pilot is of type NSManagedObject instead of Team

这是预期的行为吗?我应该修复 Xcode 生成的代码吗?我错过了什么吗?

Is this the expected behaviour? Am I supposed to fix the code generated by Xcode? Am I missing something?

顺便说一句,我使用的是 Xcode 4.3.3

BTW, I'm using Xcode 4.3.3

推荐答案

Core Data 核心是一个对象关系映射库.很久以前,它被称为企业对象框架,是 WebObjects 的一部分.

Core Data at the core is an object relational mapping library. Long time ago it was called Entreprise Object Framework, part of WebObjects.

是的,Core Data 管理的任何持久对象的基础对象都是 NSManagedObject,你可以用它们做任何你想做的事情.

So yes, the base object for any persistant object managed by Core Data is NSManagedObject, and you can do whatever you want with them.

在您的示例中,Team 和 Pilot 将共享一个公共表,您将能够使用查询同时检索 Teams 和 Pilots.就是这个想法.

In your example, Team and Pilot will share a common table, and you'll be able to use queries to retrieve Teams and Pilots at once. That's the idea.

Objective-C 继承树(如果您使用自定义类)可以镜像您定义的模型,但它不需要.您可以创建自定义 RacingActor 类,将其用作自定义 TeamPilot 类的基类,或者您可以告诉模型使用RacingActor 用于 TeamPilot 对象.如果需要,您甚至可以为 Team 和/或 Pilot 定义一个完全不相关的基类(前提是 NSManagedObject 是直接或间接的父类)到.

The Objective-C inheritance tree (if you use custom classes) can mirror the model you defined, but it doesn't need to. You can create a custom RacingActor class, use it as a base class for custom Team and Pilot classes, or you can tell the model to use RacingActor for Team and Pilot objects. You can even define a completely unrelated base class (provided NSManagedObject is a parent, directly or indirectly) for Team and / or Pilot if you want to.

然后,您可以自由地在业务逻辑中实现您需要的特定行为,无论是在控制器中还是在自定义数据类中.

You are then free to implement the specific behaviors you need in your business logic, either in controllers or in custom data classes.

这篇关于Core Data 中的抽象实体和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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