将两个 CoreData 关系(具有逆关系)分配给同一实体
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您说“意识到不可能将两种关系的逆关系设置为同一个实体”时,我可能不明白您的意思。考虑这个模型:
很明显,两个实体之间可能存在多个双向关系。在这种情况下,存在三种双向关系:
当然,您仍然需要保持 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:
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:
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...