SwiftData Query

  • Feb 25, 2024

How to use SwiftData @Query (Query) with SwiftUI

In this post, you will see SwiftUI examples for:

  • SwiftData Query

  • SwiftData Query with sort

  • SwiftData Query with filter


SwiftData Models

Here are the SwiftData models we will use for the examples.

@Model
class AuthorModel {
    var name: String
    var books: [BookModel] = []
    
    init(name: String) {
        self.name = name
    }
}

@Model
class BookModel {
    var title: String
    var author: AuthorModel?

    init(title: String, author: AuthorModel? = nil) {
        self.title = title
        self.author = author
    }
}
  • An author can have zero (empty array) or many books.

  • A book can have zero (nil) or one author.


Simple Query

  • Get all authors

  • Get all books

@Query private var authors: [AuthorModel]

@Query private var books: [BookModel]


Sorting

  • Sort authors by name

  • Sort books by title

@Query(sort: \AuthorModel.name) private var authors: [AuthorModel]

@Query(sort: \BookModel.title) private var books: [BookModel]


Filtering

  • Get all authors whose name begins with "Chase"

  • Get all books with the author of "Chase Blum"

@Query(filter: #Predicate<AuthorModel> { author in
    author.name.starts(with: "Chase")
}) private var authors: [AuthorModel]

@Query(filter: #Predicate<BookModel> {
    $0.author?.name == "Chase Blum"
}) private var books: [BookModel]

Notes

  • The second example uses "Shorthand argument names". The $0 is shorthand for the book being passed in.

  • The second example is filtering on the related model (AuthorModel).


Putting It All Together

If you need more help understanding how everything fits together within an Xcode project, you might be interested in SwiftData Mastery in SwiftUI, specifically the First Example chapter.

It will step you through how to structure your project for success with SwiftData:

SwiftData Mastery

(From: SwiftData Mastery in SwiftUI book)


Learn More With A Special Offer!

You can learn a lot more about querying data in my book SwiftData Mastery in SwiftUI.

  • How do you programmatically change sorting?

  • What if you want to use dynamic filtering with a SwiftData query?

  • How do you dynamically change sorting AND filtering?

  • How can @Query work with the SwiftUI searchable modifier?

  • Can you add animations to SwiftData queries? What are the limitations?

  • How can you go even further beyond the limits of the SwiftData @Query?

  • How can you create queries for relationships?

SwiftData Mastery

So, are you interested in advancing your skills with SwiftData easily and visually?

We offer a book to help you do just that!

We're thrilled to offer you an exclusive 11% discount on all our SwiftUI books!

📚 Use coupon code POSTS_11 at checkout or just click this link bigmountainstudio.com to claim your discount.

(Note: Coupons cannot be used during sales.)

Your Learning Path Quick Links (Save 11%)

  1. Create UI with SwiftUI Views Mastery (beginner)

  2. Architect your app using SwiftUI Essentials (beginner)

  3. Improve your app's UX with SwiftUI Animations Mastery (junior)

  4. Save & sync your app's data with SwiftData Mastery -or- Core Data Mastery (junior) 

  5. React to and manipulate data using Combine Mastery in SwiftUI (advanced)