Skip to content

Commit 89eda3e

Browse files
committed
PR feedback
1 parent 983fedf commit 89eda3e

File tree

7 files changed

+43
-56
lines changed

7 files changed

+43
-56
lines changed

README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,10 @@ Person[0..<5]
166166
When you know you'll need related objects, you can prefetch relationships to reduce trips to the persistent store:
167167

168168
```swift
169-
// Prefetch a single relationship
170-
let ordersWithCustomers = Order
171-
.include(#Predicate { $0.status == .active })
172-
.prefetchRelationship(\.customer)
173-
174169
// Prefetch multiple relationships
175170
let ordersWithDetails = Order
176-
.prefetchRelationship(\.customer)
177-
.prefetchRelationship(\.items)
171+
.include(#Predicate { $0.status == .active })
172+
.prefetchRelationships(\.customer, \.items)
178173
```
179174

180175
#### Fetching specific properties

Sources/SwiftQuery/FetchWrappers/FetchAll.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import Foundation
22
import CoreData
33
import Dependencies
44
import SwiftData
5-
#if canImport(SwiftUI)
6-
import SwiftUI
7-
#endif
85

96
@MainActor
107
@propertyWrapper
@@ -57,7 +54,3 @@ public final class FetchAll<Model: PersistentModel>: Observable {
5754
init() {}
5855
}
5956
}
60-
61-
#if canImport(SwiftUI)
62-
extension FetchAll: DynamicProperty {}
63-
#endif

Sources/SwiftQuery/FetchWrappers/FetchResults.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import Foundation
22
import CoreData
33
import Dependencies
44
import SwiftData
5-
#if canImport(SwiftUI)
6-
import SwiftUI
7-
#endif
85

96
@MainActor
107
@propertyWrapper
@@ -58,7 +55,3 @@ public final class FetchResults<Model: PersistentModel> {
5855
init() {}
5956
}
6057
}
61-
62-
#if canImport(SwiftUI)
63-
extension FetchResults: DynamicProperty {}
64-
#endif

Sources/SwiftQuery/PersistentModel+Query.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ public extension PersistentModel {
6868
}
6969

7070
public extension PersistentModel {
71-
/// Constructs an empty query over this model type and invokes ``Query/prefetchRelationship(_:)`` on that query.
72-
static func prefetchRelationship(_ keyPath: PartialKeyPath<Self>) -> Query<Self> {
73-
query().prefetchRelationship(keyPath)
71+
/// Constructs an empty query over this model type and invokes ``Query/prefetchRelationships(_:)`` on that query.
72+
static func prefetchRelationships(_ keyPaths: PartialKeyPath<Self>...) -> Query<Self> {
73+
query().prefetchRelationships(keyPaths)
7474
}
7575

7676
/// Constructs an empty query over this model type and invokes ``Query/fetchKeyPaths(_:)`` on that query.

Sources/SwiftQuery/Query.swift

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ public struct Query<T: PersistentModel> {
3737

3838
/// A range representing the number of results to skip before returning matches and maximum number of results to fetch. When `nil`, the query will return all matching results.
3939
public var range: Range<Int>?
40-
40+
41+
/// Key paths representing related models to fetch as part of this query.
4142
public var relationshipKeyPaths: [PartialKeyPath<T>] = []
42-
43+
44+
/// The subset of attributes to fetch for this entity
4345
public var propertiesToFetch: [PartialKeyPath<T>] = []
4446

4547
/// SwiftData compatible sort descriptors generated from the query's sort configuration.
@@ -298,36 +300,41 @@ public extension Query {
298300
}
299301

300302
public extension Query {
301-
/// Returns a new query that prefetches the specified relationship when executing the fetch.
303+
/// Returns a new query that prefetches the specified relationships when executing the fetch.
302304
///
303305
/// Prefetching relationships can improve performance by reducing the number of database
304-
/// trips needed when accessing related objects. Multiple relationships can be prefetched
305-
/// by chaining multiple calls to this method.
306-
///
307-
/// - Parameter keyPath: Key path to the relationship property to prefetch
308-
/// - Returns: A new query with the additional relationship marked for prefetching
309-
///
310-
/// ## Example
311-
/// ```swift
312-
/// // Prefetch single relationship
313-
/// let ordersWithCustomers = Order
314-
/// .include(#Predicate { $0.status == .active })
315-
/// .prefetchRelationship(\.customer)
306+
/// trips needed when accessing related objects.
316307
///
317-
/// // Prefetch multiple relationships
318-
/// let ordersWithDetails = Order
319-
/// .prefetchRelationship(\.customer)
320-
/// .prefetchRelationship(\.items)
321-
/// ```
322-
func prefetchRelationship(_ keyPath: PartialKeyPath<T>) -> Self {
308+
/// - Parameter keyPaths: Array of key paths to the relationships to prefetch
309+
/// - Returns: A new query with the additional relationships marked for prefetching
310+
func prefetchRelationships(_ keyPaths: [PartialKeyPath<T>]) -> Self {
323311
Query(
324312
predicate: predicate,
325313
sortBy: sortBy,
326314
range: range,
327-
prefetchRelationships: relationshipKeyPaths + [keyPath],
315+
prefetchRelationships: relationshipKeyPaths + keyPaths,
328316
propertiesToFetch: propertiesToFetch
329317
)
330318
}
319+
320+
/// Returns a new query that prefetches the specified relationships when executing the fetch.
321+
///
322+
/// Prefetching relationships can improve performance by reducing the number of database
323+
/// trips needed when accessing related objects.
324+
///
325+
/// - Parameter keyPaths: Key paths to the relationships to prefetch
326+
/// - Returns: A new query with the additional relationships marked for prefetching
327+
///
328+
/// ## Example
329+
/// ```swift
330+
/// // Prefetch multiple relationships
331+
/// let ordersWithDetails = Order
332+
/// .include(#Predicate { $0.status == .active })
333+
/// .prefetchRelationships(\.customer, \.items)
334+
/// ```
335+
func prefetchRelationships(_ keyPaths: PartialKeyPath<T>...) -> Self {
336+
prefetchRelationships(keyPaths)
337+
}
331338

332339
/// Returns a new query that fetches only the specified key paths when executing the fetch.
333340
///

Tests/SwiftQueryTests/PersistentModelQueryTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ struct PersistentModelQueryTests {
6161
#expect(modelQuery.sortDescriptors.first?.order == directQuery.sortDescriptors.first?.order)
6262
}
6363

64-
@Test func prefetchRelationship() async throws {
65-
let modelQuery = Person.prefetchRelationship(\.name)
66-
let directQuery = Query<Person>().prefetchRelationship(\.name)
64+
@Test func prefetchRelationships() async throws {
65+
let modelQuery = Person.prefetchRelationships(\.name)
66+
let directQuery = Query<Person>().prefetchRelationships(\.name)
6767

6868
#expect(modelQuery.relationshipKeyPaths.count == directQuery.relationshipKeyPaths.count)
6969
#expect(modelQuery.relationshipKeyPaths.contains(\Person.name) == directQuery.relationshipKeyPaths.contains(\Person.name))

Tests/SwiftQueryTests/QueryTests.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,29 +269,28 @@ struct QueryTests {
269269
}
270270
}
271271

272-
@Test func prefetchRelationship_single() async throws {
272+
@Test func prefetchRelationships_single() async throws {
273273
let query = Query<Person>()
274-
.prefetchRelationship(\.name)
274+
.prefetchRelationships(\.name)
275275

276276
#expect(query.relationshipKeyPaths.count == 1)
277277
#expect(query.relationshipKeyPaths.contains(\Person.name))
278278
}
279279

280-
@Test func prefetchRelationship_multiple() async throws {
280+
@Test func prefetchRelationships_multiple() async throws {
281281
let query = Query<Person>()
282-
.prefetchRelationship(\.name)
283-
.prefetchRelationship(\.age)
282+
.prefetchRelationships(\.name, \.age)
284283

285284
#expect(query.relationshipKeyPaths.count == 2)
286285
#expect(query.relationshipKeyPaths.contains(\Person.name))
287286
#expect(query.relationshipKeyPaths.contains(\Person.age))
288287
}
289288

290-
@Test func prefetchRelationship_withOtherModifiers() async throws {
289+
@Test func prefetchRelationships_withOtherModifiers() async throws {
291290
let predicate = #Predicate<Person> { $0.age >= 18 }
292291
let query = Person.include(predicate)
293292
.sortBy(\.name)
294-
.prefetchRelationship(\.age)
293+
.prefetchRelationships(\.age)
295294

296295
#expect(query.predicate != nil)
297296
#expect(query.sortBy.count == 1)

0 commit comments

Comments
 (0)