Sectioned fetching: beyond String keyPaths?

Hello, world! First and foremost, thank you for all the enhancements made to SwiftData and kudos to the team!

In Thomas’ video demonstrating “What’s new in SwiftData”, I notice the keyPath (trip.destination) used to create sections in the list is a String. I can’t help but wonder if a keyPath of another data type could be used (like trip.startDate), were one to want to create sections based on whether a trip is past or upcoming for example?

Thank you in advance for your much appreciated input!

Zoé

Answered by Frameworks Engineer in 891803022

Hi Zoé,

Both the ResultsObserver and the sectioned @Query only support string key paths for sectioning. If you have needs for different types can you file a Feedback Assistant.

However if you need to only want to do "past and upcoming" with two different @Query with a predicate to filter for upcoming and past trips - similar to Matthew's Talk.

Hi Zoé,

Both the ResultsObserver and the sectioned @Query only support string key paths for sectioning. If you have needs for different types can you file a Feedback Assistant.

However if you need to only want to do "past and upcoming" with two different @Query with a predicate to filter for upcoming and past trips - similar to Matthew's Talk.

Thank you for clarifying that only string key paths are supported for list sectioning. I’m sure being able to do this “out of the box” will be very useful.

Regarding creating sections based on whether trips are “past or upcoming”, your suggestion that I use two different queries helped me think of the problem from a different angle.

I watched Matthew’s Code-along and… Eureka! The code snippet at 16:27 - “Fetch achieved and upcoming goals” was particularly relevant and I was able to implement it 😃

Thank you for your input, much appreciated!

Sectioned fetching: beyond String keyPaths?
 
 
Q