<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: 17.0.0 -",
    "macOS: 14.0.0 -",
    "tvOS: 17.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftData",
  "identifier" : "/documentation/SwiftData/Query",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "SwiftData",
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:011_SwiftData_A2UI5QueryV"
  },
  "title" : "Query"
}
-->

# Query

A type that fetches models using the specified criteria, and manages those
models so they remain in sync with the underlying data.

```
@MainActor @preconcurrency struct Query<Element, Result> where Element : PersistentModel
```

## Topics

### Creating a query

[`init(_:animation:)`](/documentation/SwiftData/Query/init(_:animation:))

Create a query with a SwiftData fetch descriptor.

[`init(filter:sort:animation:)`](/documentation/SwiftData/Query/init(filter:sort:animation:))

Create a query with a predicate, and a list of sort descriptors.

[`init(filter:sort:order:animation:)`](/documentation/SwiftData/Query/init(filter:sort:order:animation:)-1qfoj)

Creates a query with a predicate, a key path to a property for sorting,
and the order to sort by.

[`init(filter:sort:order:animation:)`](/documentation/SwiftData/Query/init(filter:sort:order:animation:)-3qovd)

Creates a query with a predicate, a key path to a property for sorting,
and the order to sort by.

[`init(_:transaction:)`](/documentation/SwiftData/Query/init(_:transaction:))

Create a query with a SwiftData fetch descriptor.

[`init(filter:sort:transaction:)`](/documentation/SwiftData/Query/init(filter:sort:transaction:))

Create a query with a predicate, and a list of sort descriptors.

[`init(filter:sort:order:transaction:)`](/documentation/SwiftData/Query/init(filter:sort:order:transaction:)-2bx9a)

Create a query with a predicate, a key path to a property for sorting,
and the order to sort by.

[`init(filter:sort:order:transaction:)`](/documentation/SwiftData/Query/init(filter:sort:order:transaction:)-8q7vs)

Create a query with a predicate, a key path to a property for sorting,
and the order to sort by.

### Creating an unsorted, sectioned query

### Creating a sorted, sectioned query

### Getting query configuration

[`modelContext`](/documentation/SwiftData/Query/modelContext)

Current model context `Query` interacts with.

[`fetchError`](/documentation/SwiftData/Query/fetchError)

An error encountered during the most recent attempt to fetch data.

### Accessing the value

[`wrappedValue`](/documentation/SwiftData/Query/wrappedValue)

The most recent fetched result from the Query.

### Accessing sections

[`sections`](/documentation/SwiftData/Query/sections)

The sections computed from the current results, grouped by the `sectionBy` key path.

### Updating the value

## Relationships

### Conforms To

[`Sendable`](/documentation/Swift/Sendable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`DynamicProperty`](/documentation/SwiftUI/DynamicProperty)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)