If you are targeting iOS 4.0 or later and not using the Core-Data SQL store why not use predicateWithBlock:
?
The following will generate the NSFetchRequest
you want.
- (NSFetchRequest*) fetchRequestForSingleInstanceOfEntity:(NSString*)entityName groupedBy:(NSString*)attributeName
{
__block NSMutableSet *uniqueAttributes = [NSMutableSet set];
NSPredicate *filter = [NSPredicate predicateWithBlock:^(id evaluatedObject, NSDictionary *bindings) {
if( [uniqueAttributes containsObject:[evaluatedObject valueForKey:attributeName]] )
return NO;
[uniqueAttributes addObject:[evaluatedObject valueForKey:attributeName]];
return YES;
}];
NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:entityName];
req.predicate = filter;
return req;
}
You could then create a new method to execute the fetch and return the results you want.
- (NSArray*) fetchOneInstanceOfEntity:(NSString*)entityName groupedBy:(NSString*)attributeName
{
NSFetchRequest *req = [self fetchRequestForSingleInstanceOfEntity:entityName groupedBy:attributeName];
// perform fetch
NSError *fetchError = nil;
NSArray *fetchResults = [_context executeFetchRequest:req error:&fetchError];
// fetch results
if( !fetchResults ) {
// Handle error ...
return nil;
}
return fetchResults;
}