| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
#import "CoreData+JRExtensions.h" |
|---|
| 9 |
|
|---|
| 10 |
@implementation NSManagedObject (JRExtensions) |
|---|
| 11 |
|
|---|
| 12 |
+ (id)newInManagedObjectContext:(NSManagedObjectContext*)moc_ { |
|---|
| 13 |
return [[self alloc] initAndInsertIntoManagedObjectContext:moc_]; |
|---|
| 14 |
} |
|---|
| 15 |
|
|---|
| 16 |
- (id)initAndInsertIntoManagedObjectContext:(NSManagedObjectContext*)moc_ { |
|---|
| 17 |
return [NSEntityDescription insertNewObjectForEntityForName:[[[self class] entityDescriptionInManagedObjectContext:moc_] name] |
|---|
| 18 |
inManagedObjectContext:moc_]; |
|---|
| 19 |
} |
|---|
| 20 |
|
|---|
| 21 |
+ (id)rootObjectInManagedObjectContext:(NSManagedObjectContext*)moc_ error:(NSError**)error_ { |
|---|
| 22 |
NSError *error = nil; |
|---|
| 23 |
NSArray *objects = [moc_ executeFetchRequest:[self fetchRequestForEntityInManagedObjectContext:moc_] error:&error]; |
|---|
| 24 |
NSAssert( objects, @"-[NSManagedObjectContext executeFetchRequest] returned nil" ); |
|---|
| 25 |
|
|---|
| 26 |
id result = nil; |
|---|
| 27 |
|
|---|
| 28 |
switch( [objects count] ) { |
|---|
| 29 |
case 0: |
|---|
| 30 |
[[moc_ undoManager] disableUndoRegistration]; |
|---|
| 31 |
result = [self newInManagedObjectContext:moc_]; |
|---|
| 32 |
[moc_ processPendingChanges]; |
|---|
| 33 |
[[moc_ undoManager] enableUndoRegistration]; |
|---|
| 34 |
break; |
|---|
| 35 |
case 1: |
|---|
| 36 |
result = [objects objectAtIndex:0]; |
|---|
| 37 |
break; |
|---|
| 38 |
default: |
|---|
| 39 |
NSAssert2( NO, @"0 or 1 %@ objects expected, %d found", [self className], [objects count] ); |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
if (error_) { |
|---|
| 43 |
*error_ = error; |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
return result; |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
+ (id)rootObjectInManagedObjectContext:(NSManagedObjectContext*)moc_ { |
|---|
| 50 |
NSError *error = nil; |
|---|
| 51 |
id result = [self rootObjectInManagedObjectContext:moc_ error:&error]; |
|---|
| 52 |
if (error) { |
|---|
| 53 |
[NSApp presentError:error]; |
|---|
| 54 |
} |
|---|
| 55 |
return result; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
+ (NSString*)entityNameByHeuristic { |
|---|
| 59 |
NSString *result = [self className]; |
|---|
| 60 |
if( [result hasSuffix:@"MO"] ) { |
|---|
| 61 |
result = [result substringToIndex:([result length]-2)]; |
|---|
| 62 |
} |
|---|
| 63 |
return result; |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
+ (NSEntityDescription*)entityDescriptionInManagedObjectContext:(NSManagedObjectContext*)moc_ { |
|---|
| 67 |
NSEntityDescription *result = [NSEntityDescription entityForName:[self entityNameByHeuristic] inManagedObjectContext:moc_]; |
|---|
| 68 |
if( nil == result ) { |
|---|
| 69 |
|
|---|
| 70 |
NSString *className = [self className]; |
|---|
| 71 |
NSManagedObjectModel *managedObjectModel = [[moc_ persistentStoreCoordinator] managedObjectModel]; |
|---|
| 72 |
NSArray *entities = [managedObjectModel entities]; |
|---|
| 73 |
unsigned entityIndex = 0, entityCount = [entities count]; |
|---|
| 74 |
for( ; nil == result && entityIndex < entityCount; ++entityIndex ) { |
|---|
| 75 |
if( [[[entities objectAtIndex:entityIndex] managedObjectClassName] isEqualToString:className] ) { |
|---|
| 76 |
result = [entities objectAtIndex:entityIndex]; |
|---|
| 77 |
} |
|---|
| 78 |
} |
|---|
| 79 |
NSAssert1( result, @"no entity found with a managedObjectClassName of %@", className ); |
|---|
| 80 |
} |
|---|
| 81 |
return result; |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
+ (NSFetchRequest*)fetchRequestForEntityInManagedObjectContext:(NSManagedObjectContext*)moc_ { |
|---|
| 85 |
NSFetchRequest *result = [[[NSFetchRequest alloc] init] autorelease]; |
|---|
| 86 |
[result setEntity:[self entityDescriptionInManagedObjectContext:moc_]]; |
|---|
| 87 |
NSString *defaultSortKey = [self defaultSortKeyWithManagedObjectContext:moc_]; |
|---|
| 88 |
if (defaultSortKey) { |
|---|
| 89 |
[result setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:defaultSortKey ascending:YES] autorelease]]]; |
|---|
| 90 |
} |
|---|
| 91 |
return result; |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
+ (NSArray*)fetchAllInManagedObjectContext:(NSManagedObjectContext*)moc_ { |
|---|
| 95 |
NSError *error = nil; |
|---|
| 96 |
NSArray *result = [self fetchAllInManagedObjectContext:moc_ error:nil]; |
|---|
| 97 |
if (error) { |
|---|
| 98 |
[NSApp presentError:error]; |
|---|
| 99 |
} |
|---|
| 100 |
return result; |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
+ (NSArray*)fetchAllInManagedObjectContext:(NSManagedObjectContext*)moc_ error:(NSError**)error_ { |
|---|
| 104 |
return [moc_ executeFetchRequest:[self fetchRequestForEntityInManagedObjectContext:moc_] |
|---|
| 105 |
error:error_]; |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
+ (NSString*)defaultSortKeyWithManagedObjectContext:(NSManagedObjectContext*)moc_ { |
|---|
| 109 |
NSString *result = nil; |
|---|
| 110 |
NSEntityDescription *entityDesc = [self entityDescriptionInManagedObjectContext:moc_]; |
|---|
| 111 |
if (entityDesc) { |
|---|
| 112 |
result = [[entityDesc userInfo] objectForKey:@"defaultSortKey"]; |
|---|
| 113 |
if (!result && [[[entityDesc propertiesByName] allKeys] containsObject:@"position_"]) { |
|---|
| 114 |
result = @"position_"; |
|---|
| 115 |
} |
|---|
| 116 |
} |
|---|
| 117 |
return result; |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
- (NSString*)objectURLID { |
|---|
| 121 |
return [[[self objectID] URIRepresentation] absoluteString]; |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
@end |
|---|
| 125 |
|
|---|
| 126 |
@implementation NSManagedObjectContext (JRExtensions) |
|---|
| 127 |
|
|---|
| 128 |
- (NSArray*)executeFetchRequestNamed:(NSString*)fetchRequestName_ { |
|---|
| 129 |
return [self executeFetchRequestNamed:fetchRequestName_ substitutionVariables:nil]; |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
- (NSArray*)executeFetchRequestNamed:(NSString*)fetchRequestName_ error:(NSError**)error_ { |
|---|
| 133 |
return [self executeFetchRequestNamed:fetchRequestName_ substitutionVariables:nil error:error_]; |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|
| 136 |
- (NSArray*)executeFetchRequestNamed:(NSString*)fetchRequestName_ substitutionVariables:(NSDictionary*)variables_ { |
|---|
| 137 |
NSError *error = nil; |
|---|
| 138 |
NSArray *result = [self executeFetchRequestNamed:fetchRequestName_ substitutionVariables:variables_ error:&error]; |
|---|
| 139 |
if (error) { |
|---|
| 140 |
[NSApp presentError:error]; |
|---|
| 141 |
} |
|---|
| 142 |
return result; |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
#define FetchRequestSortDescriptorsSeemsBroken 1 |
|---|
| 146 |
|
|---|
| 147 |
- (NSArray*)executeFetchRequestNamed:(NSString*)fetchRequestName_ substitutionVariables:(NSDictionary*)variables_ error:(NSError**)error_ { |
|---|
| 148 |
NSManagedObjectModel *model = [[self persistentStoreCoordinator] managedObjectModel]; |
|---|
| 149 |
NSFetchRequest *fetchRequest = [model fetchRequestFromTemplateWithName:fetchRequestName_ |
|---|
| 150 |
substitutionVariables:variables_ ? variables_ : [NSDictionary dictionary]]; |
|---|
| 151 |
NSAssert1(fetchRequest, @"Can't find fetch request named \"%@\".", fetchRequestName_); |
|---|
| 152 |
|
|---|
| 153 |
NSString *defaultSortKey = nil; |
|---|
| 154 |
Class entityClass = NSClassFromString([[fetchRequest entity] managedObjectClassName]); |
|---|
| 155 |
if ([entityClass respondsToSelector:@selector(defaultSortKeyWithManagedObjectContext:)]) { |
|---|
| 156 |
defaultSortKey = [entityClass defaultSortKeyWithManagedObjectContext:self]; |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
#if !FetchRequestSortDescriptorsSeemsBroken |
|---|
| 160 |
if (defaultSortKey) { |
|---|
| 161 |
NSAssert([[fetchRequest sortDescriptors] count] == 0, @"Model-based fetch requests can't have sortDescriptors."); |
|---|
| 162 |
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:defaultSortKey ascending:YES] autorelease]]]; |
|---|
| 163 |
} |
|---|
| 164 |
#endif |
|---|
| 165 |
|
|---|
| 166 |
NSArray *result = [self executeFetchRequest:fetchRequest error:error_]; |
|---|
| 167 |
|
|---|
| 168 |
#if FetchRequestSortDescriptorsSeemsBroken |
|---|
| 169 |
if (defaultSortKey) { |
|---|
| 170 |
result = [result sortedArrayUsingDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:defaultSortKey ascending:YES] autorelease]]]; |
|---|
| 171 |
} |
|---|
| 172 |
#endif |
|---|
| 173 |
return result; |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
- (id)executeSingleResultFetchRequestNamed:(NSString*)fetchRequestName_ substitutionVariables:(NSDictionary*)variables_ error:(NSError**)error_ { |
|---|
| 177 |
id result = nil; |
|---|
| 178 |
NSError *error = nil; |
|---|
| 179 |
|
|---|
| 180 |
NSArray *objects = [self executeFetchRequestNamed:fetchRequestName_ substitutionVariables:variables_ error:&error]; |
|---|
| 181 |
NSAssert(objects, nil); |
|---|
| 182 |
|
|---|
| 183 |
if (!error) { |
|---|
| 184 |
switch ([objects count]) { |
|---|
| 185 |
case 0: |
|---|
| 186 |
|
|---|
| 187 |
break; |
|---|
| 188 |
case 1: |
|---|
| 189 |
result = [objects objectAtIndex:0]; |
|---|
| 190 |
break; |
|---|
| 191 |
default: |
|---|
| 192 |
NSAssert2(NO, @"%@: 0 or 1 objects expected, %u found", fetchRequestName_, [objects count]); |
|---|
| 193 |
} |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
if (error_) *error_ = error; |
|---|
| 197 |
return result; |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
- (id)objectWithURLID:(NSString*)url_ { |
|---|
| 201 |
NSParameterAssert(url_); |
|---|
| 202 |
NSURL *url = [NSURL URLWithString:url_]; |
|---|
| 203 |
NSAssert1(url, @"[NSURL URLWithString:@\"%@\"] failed", url_); |
|---|
| 204 |
NSManagedObjectID *objectID = [[self persistentStoreCoordinator] managedObjectIDForURIRepresentation:url]; |
|---|
| 205 |
return objectID ? [self objectRegisteredForID:objectID] : nil; |
|---|
| 206 |
} |
|---|
| 207 |
|
|---|
| 208 |
@end |
|---|