LAST CHANCE: Save 50% on all my Swift books and bundles! >>

Dynamically filtering our SwiftData query

Paul Hudson    @twostraws   

Our basic SwiftData query looks like this:

@Query(sort: \ var prospects: [Prospect]

By default that will load all Prospect model objects, sorting them by name, and while that's fine for the Everyone tab, it's not enough for the other two.

In our app, we have three instances of ProspectsView that vary only according to the FilterType property that gets passed in from our tab view. We’re already using that to set the title of each view, but we can also use it to filter our query.

Yes, we already have a default query in place, but if we add an initializer we can override that when a filter is set.

Add this initializer to ProspectsView now:

init(filter: FilterType) {
    self.filter = filter

    if filter != .none {
        let showContactedOnly = filter == .contacted

        _prospects = Query(filter: #Predicate {
            $0.isContacted == showContactedOnly
        }, sort: [SortDescriptor(\])

We've looked at creating queries manually previously, but there is one line that really stands out:

let showContactedOnly = filter == .contacted 

If that made you do a double take, break it down into two parts. First, this check:

filter == .contacted 

That will return true if filter is equal to .contacted, or false otherwise. And now this part:

let showContactedOnly =

That will assign the result of filter == .contacted to a new constant called showContactedOnly. So, if we read the whole line, it means "set showContactedOnly to true if our filter is set to .contacted." This makes our SwiftData predicate easy, because we can compare that constant directly against isContacted.

With that initializer in place, we can now create a List to loop over the resulting array. This will show both the title and email address for each prospect using a VStack – replace the existing text view in ProspectsView with this:

List(prospects) { prospect in
    VStack(alignment: .leading) {

If you run the app again you’ll see things are starting to look much better.

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

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

Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 3.3/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.