将两个 CoreData 关系(具有逆关系)分配给同一实体

发布于 2024-10-18 11:55:06 字数 657 浏览 4 评论 0原文

在我的 CoreData 模型中,我有一个与特定的 Activity 实体配对的 Event 实体。目前,我在事件活动之间有一个名为LatestActivity的可选关系。我现在希望专门为 NextActivity 创建一个新关系,但意识到不可能将这两种关系的逆关系设置为同一实体。

我理解为什么由于 SQlite 后备存储而无法实现这一点,但我想知道如何最好地解决它。经过一番谷歌搜索后,我看到的最佳解决方案如下:在我的 Event 实体中设置一个名为 Activities 的一对多关系,然后设置辅助函数获取并返回最新和下一个活动。

这很好,因为它允许我将来将多个活动附加到事件,但我对在哪里以及如何实现这些辅助函数感到困惑。将使用以下逻辑通过比较 Activity 实体中的 NSDate 来获取下一个和最新事件:

  • 最新活动表示当前日期之前的最近日期 下
  • 一个活动表示下一个活动比现在更大的日期

但是在 CoreData 中搜索这些实体的最简洁方法是什么?我应该在哪里进行搜索?

Within my CoreData model, I have an Event entity which is paired with specific Activity entities. Currently, I have an optional relationship between Event and Activity called LatestActivity. I'm now looking to create a new relationship specifically for the NextActivity but realise it is impossible to set the inverse of both relationships to the same entity.

I understand why this is not possible due to the SQlite backing store, but am wondering how best to go about it. After a bit of Googling the best solution I've seen is as follows: setup a one-to-many relationship called Activities within my Event entity, and then setup helper functions to fetch and return the latest and next activities.

This is good in that it allows me to attach multiple activities to events in the future, but I'm confused as to where and how to implement these helper functions. The next and latest events would be fetched by way of comparing an NSDate within the Activity entity using the following logic:

  • The latest activity represents the most recent date before the current date
  • The next activity represents the next activity with a date greater than now

But what's the cleanest way to search CoreData for these entities, and where should I carry out that search?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤檠 2024-10-25 11:55:06

当您说“意识到不可能将两种关系的逆关系设置为同一个实体”时,我可能不明白您的意思。考虑这个模型:

在此处输入图像描述

很明显,两个实体之间可能存在多个双向关系。在这种情况下,存在三种双向关系:

  1. 事件的活动<-->>和事件的活动<->>。 Activity 的事件(从 Event 的角度来看是一对多)
  2. Event 的lastActivity <--> Activity 的 inverseLastActivity
  3. 事件的 nextActivity <--> Activity 的 inverseNextActivity

当然,您仍然需要保持 lastActivity 和 nextActivity 是最新的 - 而且,也许您实时导出的建议可能更可靠。

我一直担心我误解了你的问题......

I may not understand you when you say that you "realise it is impossible to set the inverse of both relationships to the same entity." Consider this model:

enter image description here

It is clear that it is possible to have multiple bi-directional relationships between two entities. In this case, there are three bi-directional relationships:

  1. Event's activities <-->> Activity's event (which is to-many from Event's perspective)
  2. Event's lastActivity <--> Activity's inverseLastActivity
  3. Event's nextActivity <--> Activity's inverseNextActivity

Of course, you would still need to keep lastActivity and nextActivity up to date - and, perhaps, your suggestion of deriving that in real time may be more robust.

I have lingering concern that I have misunderstood your question...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文