diff --git a/MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalFinders.h b/MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalFinders.h index ba5aa63d7..d5726e133 100644 --- a/MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalFinders.h +++ b/MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalFinders.h @@ -30,9 +30,11 @@ + (id) MR_findFirstWithPredicate:(NSPredicate *)searchTerm andRetrieveAttributes:(NSArray *)attributes inContext:(NSManagedObjectContext *)context; + (id) MR_findFirstWithPredicate:(NSPredicate *)searchTerm sortedBy:(NSString *)sortBy ascending:(BOOL)ascending andRetrieveAttributes:(id)attributes, ...; + (id) MR_findFirstWithPredicate:(NSPredicate *)searchTerm sortedBy:(NSString *)sortBy ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context andRetrieveAttributes:(id)attributes, ...; - + (id) MR_findFirstByAttribute:(NSString *)attribute withValue:(id)searchValue; + (id) MR_findFirstByAttribute:(NSString *)attribute withValue:(id)searchValue inContext:(NSManagedObjectContext *)context; ++ (id) MR_findFirstOrderedByAttribute:(NSString *)attribute ascending:(BOOL)ascending; ++ (id) MR_findFirstOrderedByAttribute:(NSString *)attribute ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context; + + (NSArray *) MR_findByAttribute:(NSString *)attribute withValue:(id)searchValue; + (NSArray *) MR_findByAttribute:(NSString *)attribute withValue:(id)searchValue inContext:(NSManagedObjectContext *)context; + (NSArray *) MR_findByAttribute:(NSString *)attribute withValue:(id)searchValue andOrderBy:(NSString *)sortTerm ascending:(BOOL)ascending; diff --git a/MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalFinders.m b/MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalFinders.m index 01c623db8..a92eef23a 100644 --- a/MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalFinders.m +++ b/MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalFinders.m @@ -101,6 +101,21 @@ + (id) MR_findFirstByAttribute:(NSString *)attribute withValue:(id)searchValue inContext:[NSManagedObjectContext MR_contextForCurrentThread]]; } ++ (id) MR_findFirstOrderedByAttribute:(NSString *)attribute ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context; +{ + NSFetchRequest *request = [self MR_requestAllSortedBy:attribute ascending:ascending inContext:context]; + [request setFetchLimit:1]; + + return [self MR_executeFetchRequestAndReturnFirstObject:request inContext:context]; +} + ++ (id) MR_findFirstOrderedByAttribute:(NSString *)attribute ascending:(BOOL)ascending; +{ + return [self MR_findFirstOrderedByAttribute:attribute + ascending:ascending + inContext:[NSManagedObjectContext MR_contextForCurrentThread]]; +} + + (id) MR_findFirstWithPredicate:(NSPredicate *)searchTerm { return [self MR_findFirstWithPredicate:searchTerm inContext:[NSManagedObjectContext MR_contextForCurrentThread]]; diff --git a/MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalRequests.m b/MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalRequests.m index 1da9b72e6..57b8342bf 100644 --- a/MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalRequests.m +++ b/MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalRequests.m @@ -108,7 +108,10 @@ + (NSFetchRequest *) MR_requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL) + (NSFetchRequest *) MR_requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL)ascending withPredicate:(NSPredicate *)searchTerm inContext:(NSManagedObjectContext *)context { NSFetchRequest *request = [self MR_requestAllInContext:context]; - if (searchTerm) [request setPredicate:searchTerm]; + if (searchTerm) + { + [request setPredicate:searchTerm]; + } [request setFetchBatchSize:[self MR_defaultBatchSize]]; NSMutableArray* sortDescriptors = [[NSMutableArray alloc] init]; diff --git a/MagicalRecord/Core/MagicalRecordShorthand.h b/MagicalRecord/Core/MagicalRecordShorthand.h index 939027d19..04062267b 100644 --- a/MagicalRecord/Core/MagicalRecordShorthand.h +++ b/MagicalRecord/Core/MagicalRecordShorthand.h @@ -50,6 +50,8 @@ + (id) findFirstWithPredicate:(NSPredicate *)searchTerm sortedBy:(NSString *)sortBy ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context andRetrieveAttributes:(id)attributes, ...; + (id) findFirstByAttribute:(NSString *)attribute withValue:(id)searchValue; + (id) findFirstByAttribute:(NSString *)attribute withValue:(id)searchValue inContext:(NSManagedObjectContext *)context; ++ (id) findFirstOrderedByAttribute:(NSString *)attribute ascending:(BOOL)ascending; ++ (id) findFirstOrderedByAttribute:(NSString *)attribute ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context; + (NSArray *) findByAttribute:(NSString *)attribute withValue:(id)searchValue; + (NSArray *) findByAttribute:(NSString *)attribute withValue:(id)searchValue inContext:(NSManagedObjectContext *)context; + (NSArray *) findByAttribute:(NSString *)attribute withValue:(id)searchValue andOrderBy:(NSString *)sortTerm ascending:(BOOL)ascending;