28
28
29
29
@implementation NSManagedObjectContext (ActiveRecord)
30
30
31
- + (NSManagedObjectContext *)defaultContext {
31
+ + (NSManagedObjectContext *)defaultContext
32
+ {
32
33
return defaultContext;
33
34
}
34
35
35
- + (void )setDefaultContext:(NSManagedObjectContext *)newDefaultContext {
36
+ + (void )setDefaultContext:(NSManagedObjectContext *)newDefaultContext
37
+ {
36
38
[newDefaultContext retain ];
37
39
[defaultContext release ];
38
40
defaultContext = newDefaultContext;
39
41
}
40
42
41
- + (NSManagedObjectContext *)contextForCurrentThread {
43
+ + (NSManagedObjectContext *)contextForCurrentThread
44
+ {
42
45
NSAssert ([RKManagedObjectStore defaultObjectStore ], @" [RKManagedObjectStore defaultObjectStore] cannot be nil" );
43
46
return [[RKManagedObjectStore defaultObjectStore ] managedObjectContextForCurrentThread ];
44
47
}
@@ -51,109 +54,125 @@ @implementation NSManagedObject (ActiveRecord)
51
54
52
55
#pragma mark - RKManagedObject methods
53
56
54
- + (NSEntityDescription *)entity {
55
- NSString * className = [NSString stringWithCString: class_getName ([self class ]) encoding: NSASCIIStringEncoding];
57
+ + (NSEntityDescription *)entity
58
+ {
59
+ NSString *className = [NSString stringWithCString: class_getName ([self class ]) encoding: NSASCIIStringEncoding];
56
60
return [NSEntityDescription entityForName: className inManagedObjectContext: [NSManagedObjectContext contextForCurrentThread ]];
57
61
}
58
62
59
- + (NSFetchRequest *)fetchRequest {
63
+ + (NSFetchRequest *)fetchRequest
64
+ {
60
65
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc ] init ] autorelease ];
61
66
NSEntityDescription *entity = [self entity ];
62
67
[fetchRequest setEntity: entity];
63
68
return fetchRequest;
64
69
}
65
70
66
- + (NSArray *)objectsWithFetchRequest:(NSFetchRequest *)fetchRequest {
67
- NSError * error = nil ;
68
- NSArray * objects = [[NSManagedObjectContext contextForCurrentThread ] executeFetchRequest: fetchRequest error: &error];
71
+ + (NSArray *)objectsWithFetchRequest:(NSFetchRequest *)fetchRequest
72
+ {
73
+ NSError *error = nil ;
74
+ NSArray *objects = [[NSManagedObjectContext contextForCurrentThread ] executeFetchRequest: fetchRequest error: &error];
69
75
if (objects == nil ) {
70
76
RKLogError (@" Error: %@ " , [error localizedDescription ]);
71
77
}
72
78
return objects;
73
79
}
74
80
75
- + (NSUInteger )countOfObjectsWithFetchRequest:(NSFetchRequest *)fetchRequest {
76
- NSError * error = nil ;
81
+ + (NSUInteger )countOfObjectsWithFetchRequest:(NSFetchRequest *)fetchRequest
82
+ {
83
+ NSError *error = nil ;
77
84
NSUInteger objectCount = [[NSManagedObjectContext contextForCurrentThread ] countForFetchRequest: fetchRequest error: &error];
78
85
if (objectCount == NSNotFound ) {
79
86
RKLogError (@" Error: %@ " , [error localizedDescription ]);
80
87
}
81
88
return objectCount;
82
89
}
83
90
84
- + (NSArray *)objectsWithFetchRequests:(NSArray *)fetchRequests {
85
- NSMutableArray * mutableObjectArray = [[NSMutableArray alloc ] init ];
86
- for (NSFetchRequest * fetchRequest in fetchRequests) {
91
+ + (NSArray *)objectsWithFetchRequests:(NSArray *)fetchRequests
92
+ {
93
+ NSMutableArray *mutableObjectArray = [[NSMutableArray alloc ] init ];
94
+ for (NSFetchRequest *fetchRequest in fetchRequests) {
87
95
[mutableObjectArray addObjectsFromArray: [self objectsWithFetchRequest: fetchRequest]];
88
96
}
89
- NSArray * objects = [NSArray arrayWithArray: mutableObjectArray];
97
+ NSArray * objects = [NSArray arrayWithArray: mutableObjectArray];
90
98
[mutableObjectArray release ];
91
99
return objects;
92
100
}
93
101
94
- + (id )objectWithFetchRequest:(NSFetchRequest *)fetchRequest {
102
+ + (id )objectWithFetchRequest:(NSFetchRequest *)fetchRequest
103
+ {
95
104
[fetchRequest setFetchLimit: 1 ];
96
- NSArray * objects = [self objectsWithFetchRequest: fetchRequest];
105
+ NSArray * objects = [self objectsWithFetchRequest: fetchRequest];
97
106
if ([objects count ] == 0 ) {
98
107
return nil ;
99
108
} else {
100
109
return [objects objectAtIndex: 0 ];
101
110
}
102
111
}
103
112
104
- + (NSArray *)objectsWithPredicate:(NSPredicate *)predicate {
105
- NSFetchRequest * fetchRequest = [self fetchRequest ];
113
+ + (NSArray *)objectsWithPredicate:(NSPredicate *)predicate
114
+ {
115
+ NSFetchRequest *fetchRequest = [self fetchRequest ];
106
116
[fetchRequest setPredicate: predicate];
107
117
return [self objectsWithFetchRequest: fetchRequest];
108
118
}
109
119
110
- + (id )objectWithPredicate:(NSPredicate *)predicate {
111
- NSFetchRequest * fetchRequest = [self fetchRequest ];
120
+ + (id )objectWithPredicate:(NSPredicate *)predicate
121
+ {
122
+ NSFetchRequest *fetchRequest = [self fetchRequest ];
112
123
[fetchRequest setPredicate: predicate];
113
124
return [self objectWithFetchRequest: fetchRequest];
114
125
}
115
126
116
- + (NSArray *)allObjects {
127
+ + (NSArray *)allObjects
128
+ {
117
129
return [self objectsWithPredicate: nil ];
118
130
}
119
131
120
- + (NSUInteger )count:(NSError **)error {
121
- NSFetchRequest * fetchRequest = [self fetchRequest ];
132
+ + (NSUInteger )count:(NSError **)error
133
+ {
134
+ NSFetchRequest *fetchRequest = [self fetchRequest ];
122
135
return [[NSManagedObjectContext contextForCurrentThread ] countForFetchRequest: fetchRequest error: error];
123
136
}
124
137
125
- + (NSUInteger )count {
138
+ + (NSUInteger )count
139
+ {
126
140
NSError *error = nil ;
127
141
return [self count: &error];
128
142
}
129
143
130
- + (id )object {
144
+ + (id )object
145
+ {
131
146
id object = [[self alloc ] initWithEntity: [self entity ] insertIntoManagedObjectContext: [NSManagedObjectContext contextForCurrentThread ]];
132
147
return [object autorelease ];
133
148
}
134
149
135
- - (BOOL )isNew {
150
+ - (BOOL )isNew
151
+ {
136
152
NSDictionary *vals = [self committedValuesForKeys: nil ];
137
153
return [vals count ] == 0 ;
138
154
}
139
155
140
- + (id )findByPrimaryKey:(id )primaryKeyValue inContext:(NSManagedObjectContext *)context {
156
+ + (id )findByPrimaryKey:(id )primaryKeyValue inContext:(NSManagedObjectContext *)context
157
+ {
141
158
NSPredicate *predicate = [[self entityDescriptionInContext: context] predicateForPrimaryKeyAttributeWithValue: primaryKeyValue];
142
159
if (! predicate) {
143
160
RKLogWarning (@" Attempt to findByPrimaryKey for entity with nil primaryKeyAttribute. Set the primaryKeyAttributeName and try again! %@ " , self);
144
161
return nil ;
145
162
}
146
-
163
+
147
164
return [self findFirstWithPredicate: predicate inContext: context];
148
165
}
149
166
150
- + (id )findByPrimaryKey:(id )primaryKeyValue {
167
+ + (id )findByPrimaryKey:(id )primaryKeyValue
168
+ {
151
169
return [self findByPrimaryKey: primaryKeyValue inContext: [NSManagedObjectContext contextForCurrentThread ]];
152
170
}
153
171
154
172
#pragma mark - MagicalRecord Ported Methods
155
173
156
- + (NSManagedObjectContext *)currentContext; {
174
+ + (NSManagedObjectContext *)currentContext;
175
+ {
157
176
return [NSManagedObjectContext contextForCurrentThread ];
158
177
}
159
178
@@ -276,7 +295,7 @@ + (NSArray *)propertiesNamed:(NSArray *)properties
276
295
}
277
296
else
278
297
{
279
- RKLogError (@" Property '%@ ' not found in %@ properties for %@ " , propertyName, [propDict count ], NSStringFromClass (self));
298
+ RKLogError (@" Property '%@ ' not found in %d properties for %@ " , propertyName, [propDict count ], NSStringFromClass (self));
280
299
}
281
300
}
282
301
}
@@ -292,7 +311,7 @@ + (NSArray *)sortAscending:(BOOL)ascending attributes:(id)attributesToSortBy, ..
292
311
id attributeName;
293
312
va_list variadicArguments;
294
313
va_start (variadicArguments, attributesToSortBy);
295
- while ((attributeName = va_arg (variadicArguments, id ))!= nil )
314
+ while ((attributeName = va_arg (variadicArguments, id )) != nil )
296
315
{
297
316
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc ] initWithKey: attributeName ascending: ascending];
298
317
[attributes addObject: sortDescriptor];
@@ -305,7 +324,7 @@ + (NSArray *)sortAscending:(BOOL)ascending attributes:(id)attributesToSortBy, ..
305
324
{
306
325
va_list variadicArguments;
307
326
va_start (variadicArguments, attributesToSortBy);
308
- [attributes addObject: [[[NSSortDescriptor alloc ] initWithKey: attributesToSortBy ascending: ascending] autorelease ] ];
327
+ [attributes addObject: [[[NSSortDescriptor alloc ] initWithKey: attributesToSortBy ascending: ascending] autorelease ]];
309
328
va_end (variadicArguments);
310
329
}
311
330
@@ -457,7 +476,7 @@ + (NSFetchRequest *)requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL)asce
457
476
[request setIncludesSubentities: NO ];
458
477
[request setFetchBatchSize: [self defaultBatchSize ]];
459
478
460
- if (sortTerm != nil ){
479
+ if (sortTerm != nil ) {
461
480
NSSortDescriptor *sortBy = [[NSSortDescriptor alloc ] initWithKey: sortTerm ascending: ascending];
462
481
[request setSortDescriptors: [NSArray arrayWithObject: sortBy]];
463
482
[sortBy release ];
0 commit comments