|
| 1 | +// |
| 2 | +// QKQuerySet.h |
| 3 | +// QueryKit |
| 4 | +// |
| 5 | +// Created by Kyle Fuller on 30/04/2013. |
| 6 | +// |
| 7 | +// |
| 8 | + |
| 9 | +#import <Foundation/Foundation.h> |
| 10 | +#import <CoreData/CoreData.h> |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + Represents a lazy Core Data lookup for a set of objects. |
| 15 | +
|
| 16 | + This object is immutable, any changes will normally be done to a copy. Such |
| 17 | + as with the `-filter:`, `-exclude:`, `-orderBy:` and `-reverse` methods. |
| 18 | + */ |
| 19 | + |
| 20 | +@interface QKQuerySet : NSObject <NSFastEnumeration, NSCopying> |
| 21 | + |
| 22 | +/// The managed object context for the query set |
| 23 | +@property (nonatomic, strong, readonly) NSManagedObjectContext *managedObjectContext; |
| 24 | + |
| 25 | +/// The entity descriptor for the object |
| 26 | +@property (nonatomic, strong, readonly) NSEntityDescription *entityDescription; |
| 27 | + |
| 28 | +/** This is a read only property to hold any predicates set on this object. You can use the `filter:` and `exclude:` methods to effect this value on a child */ |
| 29 | +@property (nonatomic, copy, readonly) NSPredicate *predicate; |
| 30 | + |
| 31 | +/** This is a read only property to hold any sort descriptors set on this object. You can use the `orderBy:` and `reverse` methods to effect this value on a child */ |
| 32 | +@property (nonatomic, copy, readonly) NSArray *sortDescriptors; |
| 33 | + |
| 34 | +#pragma mark - Creation |
| 35 | + |
| 36 | +- (instancetype)initWithManagedObjectContext:(NSManagedObjectContext *)managedObjectContext entityDescription:(NSEntityDescription *)entityDescription __attribute((nonnull)); |
| 37 | +- (instancetype)initWithManagedObjectContext:(NSManagedObjectContext *)managedObjectContext entityDescription:(NSEntityDescription *)entityDescription predicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)sortDescriptors __attribute((nonnull(1, 2))); |
| 38 | +- (instancetype)initWithManagedObjectContext:(NSManagedObjectContext *)managedObjectContext fetchRequest:(NSFetchRequest *)fetchRequest __attribute((nonnull)); |
| 39 | + |
| 40 | +#pragma mark - Equality |
| 41 | + |
| 42 | +/** Returns a Boolean value that indicates whether a given queryset is equal to the receiver |
| 43 | + @param queryset The queryset to compare against the receiver |
| 44 | + @return YES if queryset is equivalent to the receiver |
| 45 | + */ |
| 46 | +- (BOOL)isEqualToQuerySet:(QKQuerySet *)queryset; |
| 47 | + |
| 48 | +#pragma mark - |
| 49 | + |
| 50 | +/** Returns a fetch request for the queryset */ |
| 51 | +- (NSFetchRequest *)fetchRequest; |
| 52 | + |
| 53 | +/** Returns the amount of objects matching the set predicate |
| 54 | + @param error If there is a problem fetching the count, upon return contains an instance of NSError that describes the problem. |
| 55 | + @return The number of objects matching the set predicate |
| 56 | + */ |
| 57 | +- (NSUInteger)count:(NSError **)error; |
| 58 | + |
| 59 | +/** Returns all objects matching the set predicate ordered by any set sort descriptors as an array |
| 60 | + @param error If there is a problem fetching the objects, upon return contains an instance of NSError that describes the problem. |
| 61 | + @return An array containing all matched objects |
| 62 | + */ |
| 63 | +- (NSArray *)array:(NSError **)error; |
| 64 | + |
| 65 | +/** Returns all objects matching the set predicate ordered by any set sort descriptors as an ordered set |
| 66 | + @param error If there is a problem fetching the objects, upon return contains an instance of NSError that describes the problem. |
| 67 | + @return An ordered set containing all matched objects |
| 68 | + */ |
| 69 | +- (NSSet *)set:(NSError **)error; |
| 70 | + |
| 71 | +/** Returns all objects matching the set predicate ordered by any set sort descriptors as a set |
| 72 | + @param error If there is a problem fetching the objects, upon return contains an instance of NSError that describes the problem. |
| 73 | + @return A set containing all matched objects |
| 74 | + */ |
| 75 | +- (NSOrderedSet *)orderedSet:(NSError **)error; |
| 76 | + |
| 77 | +#pragma mark - Enumeration |
| 78 | + |
| 79 | +/** Enumerate all objects matching the set predicate ordered by any set sort descriptors |
| 80 | + @param block The block to apply to elements in the array |
| 81 | + @param error If there is a problem fetching the objects, upon return contains an instance of NSError that describes the problem. |
| 82 | + @return YES if the operation succeeded. |
| 83 | + */ |
| 84 | +- (BOOL)enumerateObjects:(void (^)(NSManagedObject *object, NSUInteger index, BOOL *stop))block error:(NSError **)error; |
| 85 | + |
| 86 | +/** Enumerate all objects matching the set predicate ordered by any set sort descriptors |
| 87 | + @param block The block to apply to all objects |
| 88 | + @param error If there is a problem fetching the objects, upon return contains an instance of NSError that describes the problem. |
| 89 | + @return YES if the operation succeeded. |
| 90 | + */ |
| 91 | +- (BOOL)each:(void (^)(NSManagedObject *managedObject))block error:(NSError **)error; |
| 92 | + |
| 93 | +#pragma mark - Deletion |
| 94 | + |
| 95 | +/** Delete all objects matching the set predicate |
| 96 | + @param error If there is a problem deleting the objects, upon return contains an instance of NSError that describes the problem. |
| 97 | + @return Returns the amount of objects that were deleted |
| 98 | + */ |
| 99 | +- (NSUInteger)deleteObjects:(NSError **)error; |
| 100 | + |
| 101 | +@end |
| 102 | + |
| 103 | +/// Methods to sort an query set |
| 104 | +@interface QKQuerySet (Sorting) |
| 105 | + |
| 106 | +/** Returns a copy and the sort descriptors */ |
| 107 | +- (instancetype)orderBy:(NSArray *)sortDescriptors; |
| 108 | + |
| 109 | +/** Returns a copy and reverses any sort descriptors */ |
| 110 | +- (instancetype)reverse; |
| 111 | + |
| 112 | +@end |
| 113 | + |
| 114 | +/// Filtering related methods of QKQuerySet |
| 115 | +@interface QKQuerySet (Filtering) |
| 116 | + |
| 117 | +/** Returns a copy filtered by a predicate */ |
| 118 | +- (instancetype)filter:(NSPredicate *)predicate; |
| 119 | + |
| 120 | +/** Returns a copy excluding a predicate */ |
| 121 | +- (instancetype)exclude:(NSPredicate *)predicate; |
| 122 | + |
| 123 | +@end |
| 124 | + |
| 125 | +/// Fetching single objects in QKQuerySet |
| 126 | +@interface QKQuerySet (SingleObject) |
| 127 | + |
| 128 | +/** Returns a single object matching the filters, if there is more than one. An error will instead be returned. |
| 129 | + @param error If there is a problem fetching the object or there is more than one object, upon return contains an instance of NSError that describes the problem. |
| 130 | + @return Returns the object matching the set predicate, or nil. |
| 131 | + */ |
| 132 | +- (NSManagedObject *)object:(NSError **)error; |
| 133 | + |
| 134 | +/** Returns the first object matching the filters ordered by the set sort descriptors. |
| 135 | + @param error If there is a problem fetching the object, upon return contains an instance of NSError that describes the problem. |
| 136 | + @return Returns the first object matching the set predicate, or nil. |
| 137 | + */ |
| 138 | +- (NSManagedObject *)firstObject:(NSError **)error; |
| 139 | + |
| 140 | +/** Returns the last object matching the filters ordered by the set sort descriptors. |
| 141 | + @param error If there is a problem fetching the object, upon return contains an instance of NSError that describes the problem. |
| 142 | + @return Returns the last object matching the set predicate, or nil. |
| 143 | + */ |
| 144 | +- (NSManagedObject *)lastObject:(NSError **)error; |
| 145 | + |
| 146 | +@end |
| 147 | + |
0 commit comments