Is it possible to sort by subclasses in `NSFetchRequest` without adding additional attributes?

I want to group the results of NSFetchRequest by entity. All entities have one abstract abstract parent. For example:

 animal | |-cat | |-dog 

NSFetchRequest has includesSubentities set to TRUE and entity to animal . You can set sectionNameKeyPath for NSFetchedResultsController to entity.name , but you cannot do the same with sortDescriptors NSFetchRequest because sortDescriptors are applied to stored attributes (i.e. data in the database, not methods in 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 utility of inheritance. I looked at the SQLite database and the type of the object is stored in the same table as the attributes, so the required data is already in place.

In conclusion: is it possible to sort by subclasses in NSFetchRequest without adding additional attributes?

+7
ios iphone core-data nsfetchrequest
source share
1 answer

I think the answer is no.

The selection and sorting takes place in the store (for SQLLite storage), so the attributes must be part of the data model. From the basic Persistent Store Features data programming guide:

There are some interactions between fetching and storage type. In XML, binary, and in-memory repositories, predicate and sort descriptors are evaluated in Objective-C with access to all Cocoa functions, including comparison methods in NSString. SQL repository, on the other hand, compiles predicate and collation descriptors in SQL and evaluates the result in the database itself. This is primarily done for performance, but this means that the evaluation takes place in a non-Cocoa environment, and therefore the sort descriptors (or predicates) that rely on Cocoa cannot work.

and

In addition, you cannot sort by temporary properties using SQLite repository.

+2
source share

All Articles