是否可以在“NSFetchRequest”中按子类排序而不添加其他属性?

发布于 2024-09-27 13:50:21 字数 666 浏览 2 评论 0原文

我想按实体对 NSFetchRequest 的结果进行分组。这些实体都共享相同的抽象父级。例如:

animal
|
|-cat
|
|-dog

NSFetchRequestincludesSubentities 设置为 TRUE,将 entity 设置为 animal。可以将 NSFetchedResultsController 的 sectionNameKeyPath 设置为 entity.name,但不可能对 sortDescriptors 执行相同的操作NSFetchRequest 因为 sortDescriptors 应用于存储的属性(即数据库中的数据,而不是类上的方法)。因此,按实体类型分组的唯一方法是向超类添加一个属性,子类可以使用该属性来标识自己。

这看起来很疯狂,因为它破坏了继承的用处。我查看了 SQLite 数据库,实体类型与属性存储在同一个表中,因此所需的数据已经就位。

总之:是否可以在不添加其他属性的情况下按 NSFetchRequest 中的子类进行排序?

I want to group the results of a NSFetchRequest by entity. The entities all share the same abstract parent. For example:

animal
|
|-cat
|
|-dog

The NSFetchRequest has includesSubentities set TRUE and entity set to animal. It is possible to set sectionNameKeyPath of NSFetchedResultsController to entity.name but it is not possible to do the same with the sortDescriptors of the NSFetchRequest due to the fact that the sortDescriptors are applied to the stored attributes (i.e. data in the database, not methods on the classes). Therefore the only way to group by entity type is to add an attribute to the superclass that subclasses can use to identify themselves.

This seems crazy as it undermines the usefulness of inheritance. I had a look in the SQLite database and the entity type is stored in the same table as the attributes so the required data is already in place.

In summary: Is it possible to sort by subclasses in an NSFetchRequest without adding additional attributes?

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

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

发布评论

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

评论(1

动次打次papapa 2024-10-04 13:50:21

我想答案是否定的。

获取和排序发生在存储中(对于 SQLLite 存储),因此属性需要成为数据模型的一部分。来自核心数据编程指南(持久存储功能):

获取和存储类型之间存在一些交互。在 XML、二进制和内存存储中,谓词和排序描述符的评估是在 Objective-C 中执行的,可以访问所有 Cocoa 的功能,包括 NSString 上的比较方法。另一方面,SQL 存储将谓词和排序描述符编译为 SQL,并在数据库本身中评估结果。这样做主要是为了性能,但这意味着评估发生在非 Cocoa 环境中,因此依赖 Cocoa 的排序描述符(或谓词)无法工作。

此外,您无法使用 SQLite 存储对瞬态属性进行排序。

I think the answer is no.

Fetched and Sorting happens in the Store (for SQLLite store), so the attributes need to be part of the data model. From the core data programming guide (Persistent Store Features):

There are some interactions between fetching and the type of store. In the XML, binary, and in-memory stores, evaluation of the predicate and sort descriptors is performed in Objective-C with access to all Cocoa's functionality, including the comparison methods on NSString. The SQL store, on the other hand, compiles the predicate and sort descriptors to SQL and evaluates the result in the database itself. This is done primarily for performance, but it means that evaluation happens in a non-Cocoa environment, and so sort descriptors (or predicates) that rely on Cocoa cannot work.

also

In addition you cannot sort on transient properties using the SQLite store.

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