核心数据与同一实体的多个关系

核心数据与同一实体的多个关系

本文介绍了核心数据与同一实体的多个关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经在研究Core Data了很多,现在我决定是时候在我正在做的新项目中使用它了。



从未在工作项目中使用过它,我只是遇到了一些问题,希望获得社区的反馈。



我正在做一个基于位置的应用程序并且我想在我的核心数据模型中存储郊游,所以对于每次旅行,我都有一些传统信息,例如日期,距离,描述等。但是我还需要保存需要绘制的位置信息在地图上有些点。



所以每次旅行我都有一个 to和 from对象,所以我创建了一个MapPoint实体,其中包含纬度,经度和位置名称属性。在我的Trip实体上,我添加了一个到和来自关系,其目的地是MapPoint。



但是我如何处理逆属性?



由于Xcode似乎发出警告,我将其保留为 No inverse。



我需要在MapPoint上创建2个关系以引用Trip到目的地的关系,以及另一个引用Trip的 from关系的关系。



这是正确的吗?我不太明白



我在一个用户实体中有一个类似的问题,该问题正在其他几个实体中使用,我是否应该向使用用户的每个实体实现反向关系? / p>

要使Xcode保持快乐,似乎我需要在User上创建一个关系,使其返回Trip,并返回到我正在使用的其他实体,例如Upload,Picture实体等。 ..在我看来,以为Trip有一个User对象,然后再准备链接回与该Trip无关的Upload / Photo ...似乎令人不安。

解决方案

如果要支持 from 关系,您只需将适当的关系添加到MapPoint实体。称它们为 tripTo tripFrom 或任何您认为合适的名称,并将它们设置为<$的逆向关系。 c $ c>到的关系。



文档解释,您不需要在两个方向上都建立关系模型,但是这样做可以使生活更轻松。例如,当删除用户时会发生什么?如果您还有许多其他与User相关的实体,则需要某种方式找出与该用户相关的对象,以便您可以对其进行更新。如果您有逆向关系,Core Data可以使用您选择的删除规则(例如nullify)自动更新任何相关对象。如果没有逆向关系,则由您来解决所有相关的对象。


I've been studying Core Data quite a bit now, and I've now decided it's time to use it in a new project I'm doing.

Having never use it in a working project I've just come across a few issues I would like to get the communities feedback on.

I'm doing a location based application and I would like to store "outings" in my Core Data model, so for each trip I have some traditional information such as date, distance, description etc... But I also need to save location information which I'll need to plot some points on a map.

So I have a "to" and "from" object per trip, I've created a MapPoint entity with latitude, longitude and location name attributes. On my Trip entity, I've added a "to" and a "from" relationship who's destination is MapPoint.

But what do I do with the inverse property?

Because Xcode seems to give a warning it I leave it as "No inverse".

I needed to create 2 relationships on MapPoint to reference back to the Trip to the "to" and another relationship referencing the "from" relationship of Trip.

Is this correct ? I can't quite understand.

I have a similar issue with a User Entity where this is being used in several other Entities, should I be implementing an inverse relationship back to each Entity which uses User?

To keep Xcode happy it seems I need to create a relationship on User back to Trip, and back to other Entities I'm using such as an Upload, Picture entities etc... it seems to me disturbing to think a Trip has a User object, which would then have prepared to link back to an Upload/Photo... which has nothing to do with that Trip.

解决方案

If you want to support inverse relationships for your to and from relationships, you can just add appropriate relationships to your MapPoint entity. Call them tripTo and tripFrom, or whatever seems appropriate to you, and set those as the inverse relationships for your to and from relationships, respectively.

As the docs explain, you're not required to model a relationship in both directions, but doing so makes life easier. What happens, for example, when a user is deleted? If you have a number of other entities related to User, then you need some way to figure out which objects were related to that user so that you can update them. If you have inverse relationships, Core Data can automatically update any related objects using the deletion rule (like nullify) that you choose. Without inverse relationships, it's up to you to fix up any related objects.

这篇关于核心数据与同一实体的多个关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:12