File tree Expand file tree Collapse file tree 3 files changed +31
-1
lines changed Expand file tree Collapse file tree 3 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -177,6 +177,12 @@ let jillQuery = Person.include(#Predicate { $0.name == "Jill" })
177177let jill = jillQuery.first (in : modelContainer)
178178let lastJill = jillQuery.last (in : modelContainer)
179179```
180+ Or any result:
181+
182+ ``` swift
183+ let anyone = Person.any (in : modelContainer)
184+ ```
185+
180186
181187#### Fetching all results
182188
@@ -212,7 +218,7 @@ let jill = Person
212218 }
213219```
214220
215- ### Performing queries in a concurrency environment
221+ ### Async fetches
216222
217223Where SwiftQuery really shines is it's automatic support for performing queries
218224in a concurrency environment. The current isolation context is passed in to each function
Original file line number Diff line number Diff line change @@ -2,6 +2,12 @@ import SwiftData
22
33@MainActor
44public extension PersistentModel {
5+ /// Builds a query over this model type and invokes ``Query/first(in:)`` on that query.
6+ /// This is named `any` rather than `first` because there is no order.
7+ static func any( in container: ModelContainer ) throws -> Self ? {
8+ try query ( ) . first ( in: container)
9+ }
10+
511 /// Builds a query over this model type and invokes ``Query/results(in:)`` on that query.
612 static func results( in container: ModelContainer ) throws -> [ Self ] {
713 try query ( ) . results ( in: container)
@@ -37,6 +43,12 @@ public extension PersistentModel {
3743}
3844
3945public extension PersistentModel {
46+ /// Builds a query over this model type and invokes ``Query/first(isolation:)`` on that query.
47+ /// This is named `any` rather than `first` because there is no order.
48+ static func any( isolation: isolated ( any ModelActor ) = #isolation) throws -> Self ? {
49+ try query ( ) . first ( )
50+ }
51+
4052 /// Builds a query over this model type and invokes ``Query/results(isolation:)`` on that query.
4153 static func results( isolation: isolated ( any ModelActor ) = #isolation) throws -> [ Self ] {
4254 try query ( ) . results ( )
Original file line number Diff line number Diff line change @@ -19,6 +19,11 @@ struct PersistentModelFetchTests {
1919 try modelContainer. mainContext. save ( )
2020 }
2121
22+ @Test func any( ) throws {
23+ let modelResult = try Person . any ( in: modelContainer)
24+ #expect( modelResult != nil )
25+ }
26+
2227 @Test func results( ) throws {
2328 let modelResults = try Person . results ( in: modelContainer)
2429 let directResults = try Query < Person > ( ) . results ( in: modelContainer)
@@ -90,6 +95,13 @@ struct PersistentModelConcurrentFetchTests {
9095 try modelContainer. mainContext. save ( )
9196 }
9297
98+ @Test func any( ) async throws {
99+ try await modelContainer. createQueryActor ( ) . perform { _ in
100+ let modelResult = try Person . any ( )
101+ #expect( modelResult != nil )
102+ }
103+ }
104+
93105 @Test func results( ) async throws {
94106 try await modelContainer. createQueryActor ( ) . perform { _ in
95107 let modelResults = try Person . results ( )
You can’t perform that action at this time.
0 commit comments