@@ -80,42 +80,6 @@ public extension Query {
8080 try isolation. modelContext. fetch ( fetchDescriptor)
8181 }
8282
83- /// Provides access to a lazily-evaluated collection of objects matching the
84- /// query via a closure from within a model actor's isolation context.
85- ///
86- /// - Parameters:
87- /// - batchSize: Number of objects to fetch per batch. Defaults to 20.
88- /// - isolation: The model actor to execute the query within. Defaults to `#isolation`
89- /// which infers the current actor context.
90- /// - operation: A closure that receives the `FetchResultsCollection` for processing within
91- /// the actor's isolation domain.
92- /// - Throws: any SwiftData errors thrown during query execution
93- ///
94- /// ## Example
95- /// ```swift
96- /// // Process large result set in batches concurrently
97- /// await container.createQueryActor().perform { _ in
98- /// try Person.sortBy(\.name).fetchedResults(batchSize: 50) { results in
99- /// for person in results.prefix(100) {
100- /// // Process first 100 results efficiently
101- /// print(person.name)
102- /// }
103- /// }
104- /// }
105- /// ```
106- ///
107- /// - SeeAlso: `fetchedResults(in:batchSize:)`
108- func fetchedResults(
109- batchSize: Int = 20 ,
110- isolation: isolated ( any ModelActor ) = #isolation,
111- operation: @Sendable ( FetchResultsCollection < T > ) -> Void
112- ) throws {
113- var descriptor = fetchDescriptor
114- descriptor. includePendingChanges = false
115- let results = try isolation. modelContext. fetch ( descriptor, batchSize: batchSize)
116- operation ( results)
117- }
118-
11983 /// Returns a value computed from a lazily-evaluated collection of objects
12084 /// matching the query from within a model actor's isolation context.
12185 ///
@@ -136,15 +100,14 @@ public extension Query {
136100 /// }
137101 /// }
138102 /// ```
139- func fetchedResults< Value > (
103+ func fetchedResults(
140104 batchSize: Int = 20 ,
141- isolation: isolated ( any ModelActor ) = #isolation,
142- operation: @Sendable ( FetchResultsCollection < T > ) -> Value
143- ) throws -> Value where Value: Sendable {
105+ isolation: isolated ( any ModelActor ) = #isolation
106+ ) throws -> FetchResultsCollection < T > {
144107 var descriptor = fetchDescriptor
145108 descriptor. includePendingChanges = false
146109 let results = try isolation. modelContext. fetch ( descriptor, batchSize: batchSize)
147- return operation ( results)
110+ return results
148111 }
149112
150113 /// Returns the number of objects matching the query from within a model actor's
@@ -189,46 +152,45 @@ public extension Query {
189152 try count ( isolation: isolation) < 1
190153 }
191154
192- /// Finds an existing object matching the query, or creates a new one if none exists,
193- /// then operates on it from within a model actor's isolation context.
155+ /// Finds an existing object matching the query, or creates a new one if none exists.
156+ ///
157+ /// Can only be called from within the model actor itself because the returned results
158+ /// must remain isolated.
159+ ///
194160 ///
195161 /// - Parameters:
196162 /// - isolation: The model actor to execute the query within. Defaults to `#isolation`
197163 /// which infers the current actor context.
198164 /// - body: Closure that creates a new object if none is found
199- /// - operation: Closure that operates on the found or created object
200165 /// - Throws: `Error.missingPredicate` if the query has no predicate, or SwiftData errors
201166 ///
202167 /// ## Example
203168 /// ```swift
204169 /// // Find or create admin user concurrently
205170 /// try await container.createQueryActor().perform { actor in
206- /// try Person
171+ /// let admin = try Person
207172 /// .include(#Predicate { $0.role == "admin" })
208173 /// .findOrCreate(
209- /// body: { Person(name: "Administrator", role: "admin") },
210- /// operation: { admin in
211- /// print("Admin user: \(admin.name)")
212- /// }
174+ /// body: { Person(name: "Administrator", role: "admin") }
213175 /// )
176+ /// print("Admin user: \(admin.name)")
214177 /// }
215178 /// ```
216179 ///
217180 /// - SeeAlso: `findOrCreate(in:body:)`
218181 func findOrCreate(
219182 isolation: isolated ( any ModelActor ) = #isolation,
220- body: ( ) -> T ,
221- operation: ( T ) -> Void
222- ) throws {
183+ body: ( ) -> T
184+ ) throws -> T {
223185 guard predicate != nil else {
224186 throw Error . missingPredicate
225187 }
226188 if let found = try first ( ) {
227- operation ( found)
189+ return found
228190 } else {
229191 let created = body ( )
230192 isolation. modelContext. insert ( created)
231- operation ( created)
193+ return created
232194 }
233195 }
234196
0 commit comments