核心数据,“按瞬态属性排序”解决方法

发布于 2024-11-05 07:50:13 字数 274 浏览 5 评论 0原文

假设我有一个名为“事件”的核心数据实体,它代表周期性(每年)事件。每个事件都有一个“日期”属性。

我需要将此事件呈现给按“下一次出现的日期”排序的用户。当然,此属性取决于当前日期,因此应标记为瞬态:将其存储在数据库中是没有意义的。

但是,如您所知,您无法通过 Core Data 中的瞬态属性查询排序。

有没有一种聪明的方法可以让这个属性保持短暂状态,同时仍然对我进行核心数据排序?我不想获取然后对自己进行排序,但我也想避免将此瞬态信息存储在数据库中。

Let's say I have a Core Data entity called Event, which represents recurrent (yearly) events. Each Event has a "date" property.

I need to present this events to the user sorted by "next occurrence of date". This property, of course, depends on the current date and as such should be marked as transient: there's no point in storing it in the database.

But, as you know, you can't query sorting by a transient property in Core Data.

Is there a smart way to keep this property transient and still have Core Data sort for me? I don't want to fetch and then sort myself, but I would also like to avoid storing this transient info in the database.

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

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

发布评论

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

评论(1

冷弦 2024-11-12 07:50:13

如果您将日期存储在单独的实体中,那么您可以仅获取日期并根据需要自行对它们进行排序。您将拥有从 Event 到 EventDate 的关系,以及相应的逆关系,可让您从给定的 EventDate 查找事件。

建议:在提取请求中指定排序描述符,以便获得从年初开始排序的日期。然后您所要做的就是在返回的数组中找到当前日期,并将该点之前的所有内容移动到数组的末尾。

使 EventDate->Event 关系为多对多,因为可能会发生多个事件发生在同一日期。像这样设置模型可以为您提供一个很好的属性,您可以轻松回答“日期 X 发生了什么事件?”的问题。

If you store the date in a separate entity, then you can fetch just the dates and sort them yourself however you like. You'd have a relationship from Event to EventDate, and a corresponding inverse relationship that lets you find the Event from a given EventDate.

Suggestion: Specify a sort descriptor in your fetch request so that you get the dates sorted from the beginning of the year. Then all you have to do is locate the current date in the returned array, and move everything before that point to the end of the array.

Make the EventDate->Event relationship to-many, since it might happen that more than one Event falls on the same date. Setting your model up like this gives you the nice property that you can easily answer the question "What events happen on date X?"

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