ARTICLE AD BOX
I have a SwiftData model. For the sake of simplicity, the model looks a bit like this:
class LogItem { var timestamp: Date = Date() var itemName:String = "" var itemValue:Int = 0 }This is, as you likely guessed, an activity log. There are many itemNames. The "items" generate log entries several times within a date range in unpredictable intervals and order.
I have a SwiftUI view that shows all the logged activity in a SwiftUI List, including filtering options, and that's all working fine.
I would like to have a user-facing filter option to only display the most recent log entry for each item. Is this possible to do, either within the FetchDescriptor predicate or the List filter?
I'm aware of fetchDescriptor.fetchLimit to limit the total results. Of course setting that to 1 just gives me one total result. If I have 25 distinct itemNames, I want the list to only include 25 items, one for each itemName, sorted by either timestamp or itemName based on the user's preference.
My only thought thus far is to create another Model (or another data structure) for only the most recent items. I'd need to maintain that at all times, or created it as needed. Those will work, but I'm hoping to let SwiftData/SwiftUI do the work if there's an option to do so.
