Swift5 and CoreData: Sorting fetchRequest by related Entity-Attributes

39 Views Asked by At

Even if I run the risk of asking a question twice and I'm too blind to see:

How can I sort a FetchRequest by an Attribute of an related Entity. In my special case, I've got three tables:

Client <-> ClientToContacts <-> Contacts. In a List View I like to list all ClientToContacs sorted by Contacs.lastName.

How can I do it by sortDescriptors?

This is my FetchRequest so far. But the sortDescriptors throw errors.

sortDescriptors: [
NSSortDescriptor(keyPath: \ClientsToContacts?.toContacts!.lastName, ascending: true),
NSSortDescriptor(keyPath: \ClientsToContacts?.toContacts!.firstName, ascending: true)
],
predicate: NSPredicate(format: "(toContacts.lastName CONTAINS[cd] %@) OR (toContacts.firstName CONTAINS[cd] %@) OR (toContacts.company CONTAINS[cd] %@) OR (toContacts.department CONTAINS[cd] %@) OR (toContacts.zip CONTAINS[cd] %@)", filter, filter, filter, filter, filter),
animation: .default

The predicate works fine so far and filters my results. Only sorting...

Thanks for helping. :-)

0

There are 0 best solutions below