public
Last active

  • Download Gist
gistfile1.txt
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
+ (id)fetchFirst:(BOOL)first managedObjectWithPredicateFormat:(NSString *)format, ... {
va_list args;
va_start(args, format);
return [self fetchFirst:first managedObjectWithPrefetchedKeyPaths:nil predicateFormat:format arguments:args];
va_end(args);
}
 
+ (id)fetchFirst:(BOOL)first managedObjectWithPrefetchedKeyPaths:(NSArray *)prefetched predicateFormat:(NSString *)format, ... {
va_list args;
va_start(args, format);
return [self fetchFirst:first managedObjectWithPrefetchedKeyPaths:prefetched predicateFormat:format arguments:args];
va_end(args);
}
 
+ (id)fetchFirst:(BOOL)first managedObjectWithPrefetchedKeyPaths:(NSArray *)prefetched predicateFormat:(NSString *)format arguments:(va_list)args {
NSManagedObjectContext *context = [NSManagedObjectContext IY_threadSpecificContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSString *entityName = NSStringFromClass([self class]);
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
[fetchRequest setEntity:entity];
if (prefetched) {
[fetchRequest setRelationshipKeyPathsForPrefetching:prefetched];
}
if (format) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:format arguments:args];
[fetchRequest setPredicate:predicate];
}
NSError *error = nil;
NSArray *array = [context executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];
if (error) {
NSLog(@"%@", [error localizedDescription]);
}
if (first)
{
if ([array count] > 0) {
return [array objectAtIndex:0];
} else {
return nil;
}
} else {
return array;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.