Skip to content

Commit 967b4d7

Browse files
committed
Provide QKQuerySet, a native Objective-C QuerySet
1 parent df79327 commit 967b4d7

File tree

9 files changed

+661
-1
lines changed

9 files changed

+661
-1
lines changed

QueryKit.xcodeproj/project.pbxproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
77007D7D19A95CDE007DC2BC /* QKQuerySetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77007D7C19A95CDE007DC2BC /* QKQuerySetTests.swift */; };
11+
77007D8119A95CE9007DC2BC /* QKQuerySet.h in Headers */ = {isa = PBXBuildFile; fileRef = 77007D7E19A95CE9007DC2BC /* QKQuerySet.h */; settings = {ATTRIBUTES = (Public, ); }; };
12+
77007D8219A95CE9007DC2BC /* QKQuerySet.m in Sources */ = {isa = PBXBuildFile; fileRef = 77007D7F19A95CE9007DC2BC /* QKQuerySet.m */; };
13+
77007D8319A95CE9007DC2BC /* QKQuerySet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77007D8019A95CE9007DC2BC /* QKQuerySet.swift */; };
14+
775E2A1B19B4981E007FE5BA /* QKQuerySetTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 775E2A1A19B4981E007FE5BA /* QKQuerySetTests.m */; };
1015
77A9B67F195374490016654E /* QueryKit.h in Headers */ = {isa = PBXBuildFile; fileRef = 77A9B67E195374490016654E /* QueryKit.h */; settings = {ATTRIBUTES = (Public, ); }; };
1116
77A9B685195374490016654E /* QueryKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 77A9B679195374490016654E /* QueryKit.framework */; };
1217
77A9B68C195374490016654E /* QueryKitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77A9B68B195374490016654E /* QueryKitTests.swift */; };
@@ -57,6 +62,12 @@
5762
/* End PBXContainerItemProxy section */
5863

5964
/* Begin PBXFileReference section */
65+
77007D7C19A95CDE007DC2BC /* QKQuerySetTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QKQuerySetTests.swift; sourceTree = "<group>"; };
66+
77007D7E19A95CE9007DC2BC /* QKQuerySet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QKQuerySet.h; sourceTree = "<group>"; };
67+
77007D7F19A95CE9007DC2BC /* QKQuerySet.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QKQuerySet.m; sourceTree = "<group>"; };
68+
77007D8019A95CE9007DC2BC /* QKQuerySet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QKQuerySet.swift; sourceTree = "<group>"; };
69+
775E2A1919B4981D007FE5BA /* QueryKitTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "QueryKitTests-Bridging-Header.h"; sourceTree = "<group>"; };
70+
775E2A1A19B4981E007FE5BA /* QKQuerySetTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QKQuerySetTests.m; sourceTree = "<group>"; };
6071
77A9B679195374490016654E /* QueryKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = QueryKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6172
77A9B67D195374490016654E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
6273
77A9B67E195374490016654E /* QueryKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = QueryKit.h; sourceTree = "<group>"; };
@@ -166,14 +177,20 @@
166177
77B17B8319A94C9100D6540D /* QKAttribute.h */,
167178
77B17B8419A94C9100D6540D /* QKAttribute.m */,
168179
77B17B8719A94D2C00D6540D /* QKAttribute.swift */,
180+
77007D7E19A95CE9007DC2BC /* QKQuerySet.h */,
181+
77007D7F19A95CE9007DC2BC /* QKQuerySet.m */,
182+
77007D8019A95CE9007DC2BC /* QKQuerySet.swift */,
169183
);
170184
path = ObjectiveC;
171185
sourceTree = "<group>";
172186
};
173187
77B17B8919A94D4C00D6540D /* ObjectiveC */ = {
174188
isa = PBXGroup;
175189
children = (
190+
775E2A1919B4981D007FE5BA /* QueryKitTests-Bridging-Header.h */,
176191
77B17B8A19A94D4C00D6540D /* QKAttributeTests.swift */,
192+
775E2A1A19B4981E007FE5BA /* QKQuerySetTests.m */,
193+
77007D7C19A95CDE007DC2BC /* QKQuerySetTests.swift */,
177194
);
178195
path = ObjectiveC;
179196
sourceTree = "<group>";
@@ -187,6 +204,7 @@
187204
files = (
188205
77A9B67F195374490016654E /* QueryKit.h in Headers */,
189206
77B17B8519A94C9100D6540D /* QKAttribute.h in Headers */,
207+
77007D8119A95CE9007DC2BC /* QKQuerySet.h in Headers */,
190208
);
191209
runOnlyForDeploymentPostprocessing = 0;
192210
};
@@ -294,6 +312,8 @@
294312
77B17B8619A94C9100D6540D /* QKAttribute.m in Sources */,
295313
77B17B8819A94D2C00D6540D /* QKAttribute.swift in Sources */,
296314
77A9B698195374AA0016654E /* QueryKit.swift in Sources */,
315+
77007D8319A95CE9007DC2BC /* QKQuerySet.swift in Sources */,
316+
77007D8219A95CE9007DC2BC /* QKQuerySet.m in Sources */,
297317
77E3A05D1969C019009372A8 /* QuerySet.swift in Sources */,
298318
77E3A0611969DDF5009372A8 /* Expression.swift in Sources */,
299319
);
@@ -306,7 +326,9 @@
306326
77B17B8B19A94D4C00D6540D /* QKAttributeTests.swift in Sources */,
307327
77E8728319539C2A00A6F13F /* AttributeTests.swift in Sources */,
308328
77A9B68C195374490016654E /* QueryKitTests.swift in Sources */,
329+
77007D7D19A95CDE007DC2BC /* QKQuerySetTests.swift in Sources */,
309330
77E3A05F1969C047009372A8 /* QuerySetTests.swift in Sources */,
331+
775E2A1B19B4981E007FE5BA /* QKQuerySetTests.m in Sources */,
310332
77E8728519539FC000A6F13F /* PredicateTests.swift in Sources */,
311333
77E3A0631969E003009372A8 /* ExpressionTests.swift in Sources */,
312334
);
@@ -498,6 +520,7 @@
498520
ONLY_ACTIVE_ARCH = YES;
499521
PRODUCT_NAME = "$(TARGET_NAME)";
500522
SDKROOT = macosx;
523+
SWIFT_OBJC_BRIDGING_HEADER = "QueryKitTests/ObjectiveC/QueryKitTests-Bridging-Header.h";
501524
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
502525
};
503526
name = Debug;
@@ -541,6 +564,7 @@
541564
METAL_ENABLE_DEBUG_INFO = NO;
542565
PRODUCT_NAME = "$(TARGET_NAME)";
543566
SDKROOT = macosx;
567+
SWIFT_OBJC_BRIDGING_HEADER = "QueryKitTests/ObjectiveC/QueryKitTests-Bridging-Header.h";
544568
};
545569
name = Release;
546570
};

QueryKit/ObjectiveC/QKQuerySet.h

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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

Comments
 (0)