是否可以通过NSFetchRequest中的子类进行排序而不添加其他属性? [英] Is it possible to sort by subclasses in an `NSFetchRequest` without adding additional attributes?

查看:128
本文介绍了是否可以通过NSFetchRequest中的子类进行排序而不添加其他属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将实体的 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

NSFetchRequest include includeSubentities 设置 TRUE 实体设置为 animal 。可以将NSFetchedResultsController的 sectionNameKeyPath 设置为 entity.name ,但是不可能对<由于 sortDescriptors 是由于 NSFetchRequest 的code> sortDescriptors 应用于存储的属性(即数据库中的数据,而不是类上的方法)。因此,按实体类型分组的唯一方法是向超类添加一个属性,子类可以用它来标识自己。

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.

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

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.

总结:可以通过 NSFetchRequest 中的子类进行排序而不添加其他属性?

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

推荐答案

我认为答案是否定的。

在Store中进行获取和排序(对于SQLLite存储),因此属性需要是数据模型的一部分。从核心数据编程指南(持久存储功能):

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):


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

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.


使用SQLite存储对临时属性进行排序。

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

这篇关于是否可以通过NSFetchRequest中的子类进行排序而不添加其他属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆