核心数据与谓词的关系
我有一个实体 Car
和一个实体 Race
。这两个实体都有一个名为 carClass
的属性。我的问题是我想在通过 carClass
加入时在它们之间创建关系。
基本上,我想在核心数据中创建一个关系,该关系将代表一场比赛的可能竞争对手,具体取决于 carClass
,换句话说,如果我使用特定的 carClass
创建一场比赛我在比赛中致电竞争对手
,我想获得具有相同carClass
的汽车列表。
我的问题是,是否可以使用核心数据关系
创建,我正在考虑仅向关系添加NSPredicate
。
提前致谢。
更新
我想我没有清楚地解释我想要实现的目标。
Car{
carClass:string
}
Race{
carClass:string
competitors<-->>cars.carClass == carClass
}
并且竞争对手
应该自动获取,所以我不必手动添加竞争对手,他应该从汽车模型中检索所有匹配的汽车。
I have an entity Car
and an entity Race
. Both entities has an attribute called carClass
. My problem is that I want to create a relation between them on joining by carClass
.
Basically I want to create a relation in core data which will represents the possible competitors of a race, depending on carClass
, in other words if I create a race with a specific carClass
and I call competitors
on that race I want to get a list of cars with the same carClass
.
My question is, is this possible to create using Core Data relations
, I'm thinking about just adding an NSPredicate
to a relation.
Thanks in advance.
Update
I think i didn't explained clearly what I want to achieve.
Car{
carClass:string
}
Race{
carClass:string
competitors<-->>cars.carClass == carClass
}
And the competitors
should be fetched automatically, so I don't have to add manually the competitors, he should retrieve all the matching cars from the cars model.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的模型看起来像这样:
不要认为这是 SQL 术语,而是以对象术语来考虑。每个实体应该代表您想要模拟的现实世界对象、事件或条件。对象之间的关系应该模仿现实世界的关系。如果
Race
和Car
现实世界中的事物与CarClass
条件有共同的关系,那么您只需将其放入模型中即可。与 SQL 不同,对象模型可以任意复杂,并且它们也可以包含行为。您不仅仅局限于表格、列和行。
更新:(
请参阅 OP 中的更新以供参考)
当您发现自己将相同的属性值放入两个或多个不同的实体中时,通常意味着您需要创建一个新实体来模型该值。
在这种情况下,即使就数据而言,carClass值只是一个字符串逻辑上它完全是独立的东西与
Race
和Car
对象。因此,为了准确地对其进行建模,您需要为其提供一个单独的实体。此外,您需要对竞争对手进行建模,因此您需要一个类似于以下内容的模型:(请注意,该模型假设所有比赛均由其 carClass 定义,而不是由单个车辆或驾驶员定义。)
因此,要查找特定的所有竞争对手比赛时,您会步行
Race.carClass.cars.competitor
。这可能并不完全是您想要的,但您可以了解如何使用实体和关系来对现实世界的对象、事件或条件以及它们之间的联系进行建模。
The simplest model would look something like this:
Don't think about this is SQL terms, think of it in object terms. Each entity should represent a real-world object, event or condition that you want to simulate. The relationships between objects should be mimic the real-world relationships. If both the
Race
andCar
real-world things have a common relationship with aCarClass
condition then you simply put that in the model.Unlike SQL, object models can be arbitrarily complex and they can contain behaviors as well. Your not just stuck with tables, columns and rows.
Update:
(See update in OP for reference)
When you find yourself putting the same attribute value in two or more different entities, that usually means you need to create a new entity to model that value.
In this case, even though in terms of data the carClass value is just a string logically it is separate thing altogether that is related to both
Race
andCar
objects. So, to accurately model it, you need to provide a separate entity for it. In addition, you need to model competitors so you need a model that looks something like:(Note that this model assumes that all races are defined by their carClass and not the individual vehicles or drivers.)
So, to find all competitors of a particular race, you would walk
Race.carClass.cars.competitor
.This probably isn't exactly what you want but you can get the idea or how you use entities and relationships to model real-world objects, events or conditions and the linkages between them.
当然有可能。但是,为什么不在设置比赛时检查每辆汽车的 carClass,并在类别匹配时将汽车添加到比赛中呢?
Sure it's possible. But why not just check each cars carClass when you're setting up the race and add the car to the race if the class matches?