Skip to content

Commit

Permalink
add sort descriptors to searchFor
Browse files Browse the repository at this point in the history
  • Loading branch information
spnkr committed Oct 25, 2022
1 parent 1a991bc commit 76aa6ad
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Sources/CoreDataPlus/ManagedObjectSearchable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ public protocol ManagedObjectSearchable where Self: NSFetchRequestResult {
}

public extension ManagedObjectSearchable {
static func searchFor(_ predicate: NSPredicate?, context: NSManagedObjectContext) -> [Self] {
static func searchFor(_ predicate: NSPredicate?, sortBy: [NSSortDescriptor]? = nil, context: NSManagedObjectContext) -> [Self] {
let request = NSFetchRequest<Self>()
request.predicate = predicate
request.entity = entity()

if let sortBy = sortBy {
request.sortDescriptors = sortBy
}

return (try? context.fetch(request)) ?? []
}
}

0 comments on commit 76aa6ad

Please sign in to comment.