UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: NSSortDescriptor with NSSet count?

Forums > iOS

Is it possible to sort by the counts of one NSSet of entity?

For example:

Project

  • Items (to-many)

    In NSPredicate, it is possible to useitems.@count, but this form get error in NSSortDescriptor keypath.

    ( NSSortDescriptor(key: "items.@count", ascending: ascending))

items.count will also fail,

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'

Does it mean it is Impossible to sort by counts with NSSortDescriptor?

3      

hi,

had you considered adding a derived attribute to the entity (on the one-side of the relationship) named itemCount to keep the count of items (on the to-many side of the relationship)? that might solve the sort descriptor issue.

see WWDC2019 video Session 230, Making Apps with Core Data at around the 19:00 mark.

hope that helps,

DMG

3      

That's brilliant idea, thank you so much!

Thank you for finding the video with timestamp for me too, didn't know that before ( mainly start 2020) , now I learnt new things :)

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.