Skip to content

Commit 5c7cbe0

Browse files
committed
Add a custom fetch method for transforming fetch results with a closure
3.0-preview
1 parent 1ee1d20 commit 5c7cbe0

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

Sources/CoreDataRepository/CoreDataRepository+Fetch.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,14 @@ extension CoreDataRepository {
5555
subscription.manualFetch()
5656
}
5757
}
58+
59+
/// Fetch items from the store with a ``NSFetchRequest`` and transform the results.
60+
public func fetch<Managed, Output>(
61+
request: NSFetchRequest<Managed>,
62+
operation: @escaping (_ results: [Managed]) throws -> Output
63+
) async -> Result<Output, CoreDataError> where Managed: NSManagedObject {
64+
await context.performInChild { fetchContext in
65+
try operation(fetchContext.fetch(request))
66+
}
67+
}
5868
}

0 commit comments

Comments
 (0)