Skip to content
This repository was archived by the owner on Feb 8, 2023. It is now read-only.

Commit 6e0057f

Browse files
committed
New findFirst method that returns first result of a set
1 parent 09cbc07 commit 6e0057f

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

CoreDataKit/NSManagedObjectContext.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,26 @@ extension NSManagedObjectContext
369369
:param: predicate Predicate to filter on
370370
:param: sortDescriptors Sort descriptors to sort on
371371
:param: limit Maximum number of items to return
372+
:param: offset The number of items to skip in the result
372373

373374
:returns: Result with array of entities found, empty array on no results
374375
*/
375376
func find<T:NSManagedObject>(entityDescription: NSEntityDescription, predicate: NSPredicate? = nil, sortDescriptors: [NSSortDescriptor]? = nil, limit: Int? = nil, offset: Int? = nil) -> Result<[T]> {
376377
let fetchRequest = createFetchRequest(entityDescription, predicate: predicate, sortDescriptors: sortDescriptors, limit: limit)
377378
return executeFetchRequest(fetchRequest)
378379
}
380+
381+
/**
382+
Get the first entity that matched the given parameters
383+
384+
:param: entity Type of entity to search for
385+
:param: predicate Predicate to filter on
386+
:param: sortDescriptors Sort descriptors to sort on
387+
:param: offset The number of items to skip in the result
388+
389+
:returns: Result with the entity or result with nil if the entity is not found
390+
*/
391+
public func findFirst<T:NSManagedObject where T:NamedManagedObject>(entity: T.Type, predicate: NSPredicate? = nil, sortDescriptors: [NSSortDescriptor]? = nil, offset: Int? = nil) -> Result<T?> {
392+
return find(entity, predicate: predicate, sortDescriptors: sortDescriptors, limit: 1, offset: offset).map { $0.first }
393+
}
379394
}

0 commit comments

Comments
 (0)