I had described a way to do effective search/filtering on SwiftUI applications using CoreData, then I applied the same principles for SwiftData. However, I noticed a few issues and searched for a way to optimise these.
I had defined a list view displaying dictionary entries, as shown below.
The query is executed automatically as this view is launched and all retrieved objects are put into an array called entries. The state variable wordFilter is used to dynamically filter items based on their names. We accomplished this by attaching a .filter closure to the collection entries.
The main problem with this approach is that the entries collection is retrieved once and filtered as the user types, so the list only shows the entries that satisfy the filter criterion and as such there is no direct way to find out how many entries are shown on the view. If this is part of the information we want to display, then we have an issue. It is possible to fix this problem by using an intermediate collection that contains only the filtered items.