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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With