0

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. :-)

mihema
  • 158
  • 1
  • 10
  • You most probably don't need the `ClientsToContacts` entity at all. Core Data supports many-to-many relationships, you don't need to manually create "tables" for that. Core Data's entities are not tables and Core Data is not a database, it's a framework that manages a graph of objects. After simplifying the data model by getting rid of this unnecessary entity your task should be much easier. – Vadim Belyaev Jul 02 '22 at 13:42
  • Hi Vadim, thanks for your help. I know about many-to-many relationships. In this special case I need to store specific information about the relationship (e.g. case number) – mihema Jul 02 '22 at 13:50

0 Answers0