没有排序描述符的 NSFetchRequest

发布于 2024-09-12 06:58:42 字数 140 浏览 1 评论 0原文

如果不提供 NSSortDescriptor,我们就无法使用 NSFetchRequest。我想要做的就是获取结果并按创建顺序显示它们。有没有内置的方法可以做到这一点?或者我必须创建一个新的“自动增量”字段? (在我看来,这违背了 CoreData 所代表的一切)。

We cannot use NSFetchRequest without providing NSSortDescriptor(s). All i want to do is fetch the results and show them in the order in which they were created. Is there a built-in way to do that?, or will i have to create a new "auto-increment" field? (which goes against everything CoreData stands for, in my opinion).

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

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

发布评论

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

评论(3

混浊又暗下来 2024-09-19 06:58:42

Core Data 不保证任何有关顺序的信息。您不必创建“自动增量”字段;但是,您可以创建适当的属性。由于您显然关心创建某些内容的日期,因此您应该向数据对象添加 dateCreated 属性。然后,按此排序。

您可以在托管对象的 didAwakeFromInsert 方法中轻松设置它。

Core Data does not guarantee anything about order. You don't have to make an "auto-increment" field; however, you can make an appropriate attribute. Since you obviously care about the date something was created, you should add a dateCreated attribute to your data object. Then, sort by that.

You can easily set that in the managed object's didAwakeFromInsert method.

深陷 2024-09-19 06:58:42

听起来您正在从表的角度考虑核心数据,并期望应该有一个自动排序,就像表提供的行号一样。但是,Core Data 不使用表、行或任何其他固定的线性数据结构。

没有内置的提取顺序,因为每个提取请求都高度特定于它帮助填充的视图的需求。 NSFetchRequest 需要一个排序描述符,因为它返回一个数组,并且需要一个排序描述符将对象图中的无序托管对象转换为有序数组。

一般来说,对象图中的托管对象没有内置顺序,因为每个数据模型都有不同的数据结构化标准。例如,对于大多数模型,插入对象的时间序列是无关的。当只在极少数情况下需要时,为什么要在每个地方的每个 Core Data 应用程序都必须使用的每个托管对象中构建时间戳呢?

如果时间序列是模型逻辑关系的重要组成部分,即它反映了数据模型模拟的真实对象、事件或条件,正如 Jason Coco 建议的,您应该向实体本身添加时间戳属性,以便实体对象能够对它们自己的创建时间进行建模。

It sounds like you're thinking of Core Data in terms of tables and expect that there should be an automatic ordering just like the row number of table provides. However, Core Data does not use tables, rows or any other fixed, linear data structure.

There is no built-in order for fetches because each fetch request is highly specific to the needs of the view it helps populate. NSFetchRequest requires a sort descriptor because it returns an array and requires a sort descriptor to turn the unordered managed objects within the object graph into an ordered array.

There is no built-in order for managed objects in the object graph in general because each data model has different criteria for structuring the data. E.g for most models, the temporal sequences in which objects are inserted is irrelevant. Why build in a timestamp into every managed object that every Core Data app everywhere must use when it will only be needed in a tiny minority of cases?

If the temporal sequences is an important part of your model's logical relationships i.e. it reflects the real objects, events or conditions that the data model simulates then, as Jason Coco suggested, you should add a timestamp attribute to the entity itself such that the entities objects will model the time of their own creation.

初懵 2024-09-19 06:58:42
[[NSSortDescriptor alloc] initWithKey:@"" ascending:NO];

在我的项目中,我使用 NSFetchRequest 来获取列表,列表中的所有对象都有一个具有相同值的属性。所以我们使用该属性的名称“initWithKey:”。效果很好。

[[NSSortDescriptor alloc] initWithKey:@"" ascending:NO];

In my project, I use NSFetchRequest to fetch a list, and all objects in the list have a property which have the same value. so we use that property's name of the "initWithKey:" . It work well.

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