-
Notifications
You must be signed in to change notification settings - Fork 0
/
TIManagedObjectExtensions.m
253 lines (201 loc) · 11.6 KB
/
TIManagedObjectExtensions.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright (c) 2010 Tim Isted
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "TIManagedObjectExtensions.h"
@implementation NSManagedObject (TIManagedObjectExtensions)
#pragma mark
#pragma mark Entity Information
+ (NSString *)ti_entityName
{
NSString *nameOfClass = NSStringFromClass([self class]);
return [nameOfClass substringFromIndex:2];
}
+ (NSEntityDescription *)ti_entityDescriptionInManagedObjectContext:(NSManagedObjectContext *)aContext
{
return [NSEntityDescription entityForName:[self ti_entityName] inManagedObjectContext:aContext];
}
#pragma mark -
#pragma mark Creating Objects
+ (id)ti_objectInManagedObjectContext:(NSManagedObjectContext *)aContext
{
return [NSEntityDescription insertNewObjectForEntityForName:[self ti_entityName] inManagedObjectContext:aContext];
}
#pragma mark -
#pragma mark Fetch Requests
+ (NSFetchRequest *)ti_fetchRequestInManagedObjectContext:(NSManagedObjectContext *)aContext
{
return [self ti_fetchRequestWithPredicate:nil inManagedObjectContext:aContext];
}
+ (NSFetchRequest *)ti_fetchRequestWithPredicate:(NSPredicate *)aPredicate inManagedObjectContext:(NSManagedObjectContext *)aContext
{
return [self ti_fetchRequestWithPredicate:aPredicate inManagedObjectContext:aContext sortedWithDescriptors:nil];
}
+ (NSFetchRequest *)ti_fetchRequestWithPredicate:(NSPredicate *)aPredicate inManagedObjectContext:(NSManagedObjectContext *)aContext sortedByKey:(NSString *)aKey ascending:(BOOL)yesOrNo
{
NSSortDescriptor *sortDescriptor = nil;
if( aKey ) sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:aKey ascending:yesOrNo] autorelease];
return [self ti_fetchRequestWithPredicate:aPredicate inManagedObjectContext:aContext sortedWithDescriptor:sortDescriptor];
}
+ (NSFetchRequest *)ti_fetchRequestWithPredicate:(NSPredicate *)aPredicate inManagedObjectContext:(NSManagedObjectContext *)aContext sortedWithDescriptor:(NSSortDescriptor *)aDescriptor
{
NSArray *sortDescriptors = nil;
if( aDescriptor ) sortDescriptors = [NSArray arrayWithObject:aDescriptor];
return [self ti_fetchRequestWithPredicate:aPredicate inManagedObjectContext:aContext sortedWithDescriptors:sortDescriptors];
}
+ (NSFetchRequest *)ti_fetchRequestWithPredicate:(NSPredicate *)aPredicate inManagedObjectContext:(NSManagedObjectContext *)aContext sortedWithDescriptors:(NSArray *)someDescriptors
{
NSFetchRequest *requestToReturn = [[NSFetchRequest alloc] init];
[requestToReturn setEntity:[self ti_entityDescriptionInManagedObjectContext:aContext]];
if( aPredicate ) [requestToReturn setPredicate:aPredicate];
if( someDescriptors ) [requestToReturn setSortDescriptors:someDescriptors];
return [requestToReturn autorelease];
}
+ (NSFetchRequest *)ti_fetchRequestInManagedObjectContext:(NSManagedObjectContext *)aContext withPredicateWithFormat:(NSString *)aFormat, ...
{
va_list args;
va_start(args, aFormat);
NSPredicate *predicate = [NSPredicate predicateWithFormat:aFormat arguments:args];
va_end(args);
return [self ti_fetchRequestWithPredicate:predicate inManagedObjectContext:aContext];
}
#pragma mark -
#pragma mark Counting Objects
+ (NSUInteger)ti_numberOfObjectsInManagedObjectContext:(NSManagedObjectContext *)aContext error:(NSError **)outError
{
return [self ti_numberOfObjectsMatchingPredicate:nil inManagedObjectContext:aContext error:outError];
}
+ (NSUInteger)ti_numberOfObjectsMatchingPredicate:(NSPredicate *)aPredicate inManagedObjectContext:(NSManagedObjectContext *)aContext error:(NSError **)outError
{
NSFetchRequest *countRequest = [self ti_fetchRequestWithPredicate:aPredicate inManagedObjectContext:aContext];
NSError *anyError = nil;
NSUInteger count = [aContext countForFetchRequest:countRequest error:&anyError];
if( outError && anyError ) *outError = anyError;
return count;
}
+ (NSUInteger)ti_numberOfObjectsInManagedObjectContext:(NSManagedObjectContext *)aContext error:(NSError **)outError matchingPredicateWithFormat:(NSString *)format, ...
{
va_list args;
va_start(args, format);
NSPredicate *thePredicate = [NSPredicate predicateWithFormat:format arguments:args];
va_end(args);
return [self ti_numberOfObjectsMatchingPredicate:thePredicate inManagedObjectContext:aContext error:outError];
}
#pragma mark -
#pragma mark Fetching Objects
#pragma mark - All Objects
+ (NSArray *)ti_allObjectsInManagedObjectContext:(NSManagedObjectContext *)aContext sortedByKey:(NSString *)aKey ascending:(BOOL)yesOrNo error:(NSError **)outError
{
return [self ti_objectsMatchingPredicate:nil inManagedObjectContext:aContext sortedByKey:aKey ascending:yesOrNo error:outError];
}
+ (NSArray *)ti_allObjectsInManagedObjectContext:(NSManagedObjectContext *)aContext sortedWithDescriptor:(NSSortDescriptor *)aDescriptor error:(NSError **)outError
{
return [self ti_objectsMatchingPredicate:nil inManagedObjectContext:aContext sortedWithDescriptor:aDescriptor error:outError];
}
+ (NSArray *)ti_allObjectsInManagedObjectContext:(NSManagedObjectContext *)aContext sortedWithDescriptors:(NSArray *)someDescriptors error:(NSError **)outError
{
return [self ti_objectsMatchingPredicate:nil inManagedObjectContext:aContext sortedWithDescriptors:someDescriptors error:outError];
}
+ (NSArray *)ti_allObjectsInManagedObjectContext:(NSManagedObjectContext *)aContext error:(NSError **)outError
{
return [self ti_allObjectsInManagedObjectContext:aContext sortedWithDescriptor:nil error:outError];
}
#pragma mark - Matching Predicate
+ (NSArray *)ti_objectsMatchingPredicate:(NSPredicate *)aPredicate inManagedObjectContext:(NSManagedObjectContext *)aContext sortedWithDescriptors:(NSArray *)someDescriptors limit:(NSUInteger)fetchLimit error:(NSError **)outError
{
NSError *anyError = nil;
NSFetchRequest *request = [self ti_fetchRequestWithPredicate:aPredicate inManagedObjectContext:aContext sortedWithDescriptors:someDescriptors];
if( fetchLimit > 0 ) {
[request setFetchLimit:fetchLimit];
}
NSArray *results = [aContext executeFetchRequest:request error:&anyError];
if( !results && outError )
*outError = anyError;
return results;
}
+ (NSArray *)ti_objectsMatchingPredicate:(NSPredicate *)aPredicate inManagedObjectContext:(NSManagedObjectContext *)aContext sortedWithDescriptors:(NSArray *)someDescriptors error:(NSError **)outError
{
return [self ti_objectsMatchingPredicate:aPredicate inManagedObjectContext:aContext sortedWithDescriptors:someDescriptors limit:0 error:outError];
}
+ (NSArray *)ti_objectsMatchingPredicate:(NSPredicate *)aPredicate inManagedObjectContext:(NSManagedObjectContext *)aContext sortedWithDescriptor:(NSSortDescriptor *)aDescriptor error:(NSError **)outError
{
NSArray *sortDescriptors = nil;
if( aDescriptor ) sortDescriptors = [NSArray arrayWithObject:aDescriptor];
return [self ti_objectsMatchingPredicate:aPredicate inManagedObjectContext:aContext sortedWithDescriptors:sortDescriptors error:outError];
}
+ (NSArray *)ti_objectsMatchingPredicate:(NSPredicate *)aPredicate inManagedObjectContext:(NSManagedObjectContext *)aContext sortedByKey:(NSString *)aKey ascending:(BOOL)yesOrNo error:(NSError **)outError
{
NSSortDescriptor *sortDescriptor = nil;
if( aKey ) sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:aKey ascending:yesOrNo] autorelease];
return [self ti_objectsMatchingPredicate:aPredicate inManagedObjectContext:aContext sortedWithDescriptor:sortDescriptor error:outError];
}
+ (NSArray *)ti_objectsMatchingPredicate:(NSPredicate *)aPredicate inManagedObjectContext:(NSManagedObjectContext *)aContext error:(NSError **)outError
{
return [self ti_objectsMatchingPredicate:aPredicate inManagedObjectContext:aContext sortedWithDescriptors:nil error:outError];
}
+ (NSArray *)ti_objectsInManagedObjectContext:(NSManagedObjectContext *)aContext error:(NSError **)outError matchingPredicateWithFormat:(NSString *)aFormat, ...
{
va_list args;
va_start(args, aFormat);
NSPredicate *thePredicate = [NSPredicate predicateWithFormat:aFormat arguments:args];
va_end(args);
return [self ti_objectsMatchingPredicate:thePredicate inManagedObjectContext:aContext error:outError];
}
+ (NSArray *)ti_objectsInManagedObjectContext:(NSManagedObjectContext *)aContext sortedWithDescriptor:(NSSortDescriptor *)aDescriptor error:(NSError **)outError matchingPredicateWithFormat:(NSString *)aFormat, ...
{
va_list args;
va_start(args, aFormat);
NSPredicate *thePredicate = [NSPredicate predicateWithFormat:aFormat arguments:args];
va_end(args);
return [self ti_objectsMatchingPredicate:thePredicate inManagedObjectContext:aContext sortedWithDescriptor:aDescriptor error:outError];
}
+ (NSArray *)ti_objectsInManagedObjectContext:(NSManagedObjectContext *)aContext sortedByKey:(NSString *)aKey ascending:(BOOL)yesOrNo error:(NSError **)outError matchingPredicateWithFormat:(NSString *)aFormat, ...
{
va_list args;
va_start(args, aFormat);
NSPredicate *thePredicate = [NSPredicate predicateWithFormat:aFormat arguments:args];
va_end(args);
return [self ti_objectsMatchingPredicate:thePredicate inManagedObjectContext:aContext sortedByKey:aKey ascending:yesOrNo error:outError];
}
+ (NSArray *)ti_objectsInManagedObjectContext:(NSManagedObjectContext *)aContext sortedWithDescriptors:(NSArray *)someDescriptors error:(NSError **)outError matchingPredicateWithFormat:(NSString *)aFormat, ...
{
va_list args;
va_start(args, aFormat);
NSPredicate *thePredicate = [NSPredicate predicateWithFormat:aFormat arguments:args];
va_end(args);
return [self ti_objectsMatchingPredicate:thePredicate inManagedObjectContext:aContext sortedWithDescriptors:someDescriptors error:outError];
}
#pragma mark - First Object
+ (id)_ti_firstObjectInArrayIfExists:(NSArray *)anArray
{
if( [anArray count] < 1 ) return nil;
return [anArray objectAtIndex:0];
}
+ (id)ti_firstObjectMatchingPredicate:(NSPredicate *)aPredicate inManagedObjectContext:(NSManagedObjectContext *)aContext error:(NSError **)outError
{
return [self _ti_firstObjectInArrayIfExists:[self ti_objectsMatchingPredicate:aPredicate inManagedObjectContext:aContext sortedWithDescriptors:nil limit:1 error:outError]];
}
+ (id)ti_firstObjectInManagedObjectContext:(NSManagedObjectContext *)aContext error:(NSError **)outError matchingPredicateWithFormat:(NSString *)aFormat, ...
{
va_list args;
va_start(args, aFormat);
NSPredicate *thePredicate = [NSPredicate predicateWithFormat:aFormat arguments:args];
va_end(args);
return [self ti_firstObjectMatchingPredicate:thePredicate inManagedObjectContext:aContext error:outError];
}
@end