title: Count Distinct items in Core Data
body: <p>I have a Core Data entity with a property named 'value' which is often repeated. I wish to retrieve only unique values (done) and also how often each one appears, so that I can sort by that property (I'm building an autocomplete function based on existing user input, so knowing how frequently a certain input has appeared is essential).</p>
  
  <p>My fetch request currently looks like this:</p>
  
  <pre><code>NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
  NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:self.managedObjectContext];
  NSDictionary *entityProperties = [entity propertiesByName];
  [fetchRequest setEntity:entity];
  
  [fetchRequest setFetchBatchSize:10];
  [fetchRequest setFetchLimit:20];
  [fetchRequest setReturnsDistinctResults:YES];
  [fetchRequest setResultType:NSDictionaryResultType];
  
  [fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:[entityProperties objectForKey:@"value"]]];
  
  NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"value" ascending:YES] autorelease];
  NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
  [fetchRequest setSortDescriptors:sortDescriptors];
  
  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"value BEGINSWITH[c] %@",predicateString];
  [fetchRequest setPredicate:predicate];
  
  return fetchRequest;
  </code></pre>
  
  <p>I'm a bit stuck with this one. Any ideas? </p>
  
  <p>Thanks in advance!</p>