Skip to content

Commit

Permalink
fetchFirst
Browse files Browse the repository at this point in the history
  • Loading branch information
patron-ghigley committed Oct 27, 2022
1 parent 3f529f8 commit d379482
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Sources/CoreDataQueryInterface/QueryBuilder+Fetch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public extension QueryBuilder {
try dictionaries().fetch(managedObjectContext) as! [[String: Any]]
}

func fetchFirst(_ managedObjectContext: NSManagedObjectContext? = nil) throws -> R? {
try limit(1).fetch(managedObjectContext).first
}

func count(_ managedObjectContext: NSManagedObjectContext? = nil) throws -> Int {
guard let moc = self.managedObjectContext ?? managedObjectContext else {
preconditionFailure("No NSManagedObjectContext instance on which to execute the request.")
Expand Down
2 changes: 1 addition & 1 deletion Tests/CoreDataQueryInterfaceTests/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ enum Store {
let languageNames = info["ls"] as! [String]
var languages: Set<Language> = []
for name in languageNames {
let language = try! moc.query(Language.self).filter { $0.name == name }.fetch().first!
let language = try! moc.query(Language.self).filter { $0.name == name }.fetchFirst()!
languages.insert(language)
}
let developer = Developer(context: moc)
Expand Down

0 comments on commit d379482

Please sign in to comment.