是否可以在“NSFetchRequest”中按子类排序而不添加其他属性?
我想按实体对 NSFetchRequest
的结果进行分组。这些实体都共享相同的抽象父级。例如:
animal
|
|-cat
|
|-dog
NSFetchRequest
将 includesSubentities
设置为 TRUE
,将 entity
设置为 animal
。可以将 NSFetchedResultsController 的 sectionNameKeyPath
设置为 entity.name
,但不可能对 的
因为 sortDescriptors
执行相同的操作NSFetchRequestsortDescriptors
应用于存储的属性(即数据库中的数据,而不是类上的方法)。因此,按实体类型分组的唯一方法是向超类添加一个属性,子类可以使用该属性来标识自己。
这看起来很疯狂,因为它破坏了继承的用处。我查看了 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想答案是否定的。
获取和排序发生在存储中(对于 SQLLite 存储),因此属性需要成为数据模型的一部分。来自核心数据编程指南(持久存储功能):
还
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):
also