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

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

问题描述

我有一个有3个实体的Formula 1比赛的数据模型:




  • RacingActor:抽象实体

  • Pilot:继承了RacingActor

  • 团队:继承自RacingActor





如果我生成NSManagedObject子类来表示这些实体,生成的代码并不代表此设计:




  • 一切继承自NSManagedObject

  • 没有什么阻止我实例化RacingActor

  • Pilot中的team属性的类型为NSManagedObject,而不是Team



这是预期的行为吗?我应该修复由Xcode生成的代码?

解决方案



div>

Core数据在核心是一个对象关系映射库。很久以前,它被称为Entreprise对象框架,WebObjects的一部分。



是的,Core Data管理的任何持久化对象的基础对象是 NSManagedObject ,你可以做任何你想要的。



在你的例子中,Team和Pilot会共享一个公共表,我们可以使用查询来一次检索小组和飞行员。这是个想法。



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



然后,您可以在控制器或自定义数据类中自由实现业务逻辑中需要的特定行为。


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

  • RacingActor: Abstract entity
  • Pilot: inherits from RacingActor
  • Team: inherits from RacingActor

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

  • Everything inherits from NSManagedObject
  • Nothing prevents me from instantiating RacingActor
  • The team property in Pilot is of type NSManagedObject instead of Team

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

BTW, I'm using Xcode 4.3.3

解决方案

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

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

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.

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天全站免登陆