How to sort by relationship? #109

Closed
opened 2025-12-29 18:23:08 +01:00 by adam · 2 comments
Owner

Originally created by @ghost on GitHub (Dec 13, 2016).

I have an entity Person, each person has many messages. I wish to sort my tableview based on the last message's timestamp per each Person. Essentially, accomplish something like this .
let People: ListMonitor = {

          return Static.datastack.monitorSectionedList(
        
               From<Person>(),
               SectionBy(#keyPath(Person.section)),
               Where("messages.@count > 0"),
               OrderBy(.descending(#keyPath(Person.messages.last.timestamp))) <--------
        
    )
    
}()
Originally created by @ghost on GitHub (Dec 13, 2016). I have an entity Person, each person has many messages. I wish to sort my tableview based on the last message's timestamp per each Person. Essentially, accomplish something like this . let People: ListMonitor<Person> = { return Static.datastack.monitorSectionedList( From<Person>(), SectionBy(#keyPath(Person.section)), Where("messages.@count > 0"), OrderBy(.descending(#keyPath(Person.messages.last.timestamp))) <-------- ) }()
adam added the question label 2025-12-29 18:23:08 +01:00
adam closed this issue 2025-12-29 18:23:08 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Dec 14, 2016):

Unfortunately, Core Data doesn't support sorting lists with relationships. My suggestion is to add a messageCount attribute in the Person entity which you update whenever a message is added to a person. You can then use that attribute to sort.

@JohnEstropia commented on GitHub (Dec 14, 2016): Unfortunately, Core Data doesn't support sorting lists with relationships. My suggestion is to add a `messageCount` attribute in the `Person` entity which you update whenever a message is added to a person. You can then use that attribute to sort.
Author
Owner

@ghost commented on GitHub (Dec 15, 2016):

Thank you John, just making sure I wasn't missing out on a feature. I guess that should work !

@ghost commented on GitHub (Dec 15, 2016): Thank you John, just making sure I wasn't missing out on a feature. I guess that should work !
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore-JohnEstropia#109