0Pricing
SwiftUI Academy · Lesson

Querying with @Query

Fetch and sort persisted data in views.

Querying with @Query is a free SwiftUI Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Fetching Saved Data

To show stored records in a view, you fetch them with @Query. It reads from your container and keeps the view fresh. 🔍

The @Query Property

Declare an array property with @Query and SwiftData fills it with every matching record. No manual loading needed.

@Query private var tasks: [Task]

Showing Results in a List

Pair @Query with a List to render rows from your data. Each saved object becomes one line on screen.

List(tasks) { task in
    Text(task.title)
}

Live Updates

A @Query view updates by itself when data changes. Insert a record and the list redraws with no extra work.

Sorting Results

Pass a sort key path to order your results. Here tasks come back alphabetically by title every time.

@Query(sort: \.title) var tasks: [Task]

Sort Direction

Add order to sort ascending or descending. Newest-first lists feel natural for things like recent notes.

@Query(sort: \.due, order: .reverse)
var tasks: [Task]

Filtering with Predicates

A #Predicate narrows results to records that match a rule. Only the rows you care about reach the view.

#Predicate<Task> { $0.isDone == false }

Applying a Filter

Pass your predicate to @Query to fetch a filtered set. This view shows only the tasks still left to do.

@Query(filter: #Predicate<Task> {
    !$0.isDone })
var open: [Task]

Multiple Sort Keys

Give an array of SortDescriptor values to sort by more than one field. Tie-breaks make long lists feel ordered.

@Query(sort: [SortDescriptor(\.priority)])
var tasks: [Task]

Counting and Empty States

Check the array's isEmpty to show a friendly placeholder. A clear empty state guides new users gently.

if tasks.isEmpty {
    Text("No tasks yet")
}

Queries Stay in Sync

Because @Query watches the store, your UI never drifts from the data. Save once and every query reflects it.

Quick Check

Quick check on querying data.

Recap: Querying

You used @Query to fetch, sort, and filter saved data, and watched the view stay live. Reading from SwiftData is now second nature. 🎯

Frequently asked questions

Is the “Querying with @Query” lesson free?

Yes — the full text of “Querying with @Query” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.

What will I learn in “Querying with @Query”?

Fetch and sort persisted data in views. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start SwiftUI Academy?

No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Querying with @Query” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this SwiftUI Academy lesson?

Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining @Model Types
  2. The Model Container & Context
  3. Querying with @Query
  4. Inserting, Updating & Deleting
← Back to SwiftUI Academy