7

I have a fetched results controller that should display all items of a certain entity that has a number of subentities.

The sections in the fetched results controller should be based on the entity name, i.e which subentity an item belongs to. Setting the sectionNameKeyPath to @"entity.name" works.

It seems, however, to be impossible to get the right sort descriptor for the fetch request. Things like [NSSortDescriptor sortDescriptorWithKey:@"entity.name" ascending:YES] result in errors like keypath entity.name not found in entity Something.

mrueg
  • 8,185
  • 4
  • 44
  • 66

1 Answers1

3

Try using [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; You don't need the preceding entitiy, since the entitiy ist already defined by the fetch request. See also the Sort Descriptor Class Reference.

Björn Landmesser
  • 953
  • 10
  • 26
  • 1
    +1 Definitely the issue. `entity.name` means "look for a relationship named entity and then ask the object on the other side of the relationship for its `name` attribute. Since there is no `entity` relationship the sort fails. – TechZen Jun 21 '11 at 20:14
  • This results in `keypath name not found in entity Something`, the reason for which is obviously that my entity Something does not have a property called name. Even if it did, that wouldn't result in the correct sorting. A somewhat different formulation of my question can be found at http://stackoverflow.com/questions/3931442/is-it-possible-to-sort-by-subclasses-in-an-nsfetchrequest-without-adding-additi – mrueg Jun 28 '11 at 13:56
  • @ TechZen: What `entity.name` _should_ mean, is "look for _some_ property named entity (i.e not just relationships) and ask the resulting object for its name". Only problem with this is that the sorting is performed in SQLite and not on the NSManagedObjects. – mrueg Jun 28 '11 at 13:59