title: ios: NSFetchRequest and filtering
body: <p>I have a NSFetchRequest (executing in a NSFetchedResultsController) for the following data:</p>
  
  <pre><code>Person (one-to-many) Encounter
  </code></pre>
  
  <p>Encounter has an int field "type". In my tableView, I want to show:</p>
  
  <pre><code>Person A - Encounter of type 1
  Person A - Encounter of type 2
  Person B - Encounter of type 1
  </code></pre>
  
  <p>etc. That is, for a Person, I only want one Encounter of each type. Is there a way to do that in a Core Data query? I could do this with code to filter the result of an NSFetchRequest, but then I couldn't use NSFetchedResultsController.</p>
  
  <p>[EDIT]</p>
  
  <p>Here's the code I'm currently trying. The result is several Encounters with the same type for one Person.</p>
  
  <pre><code>NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  // entity
  NSEntityDescription *entity = [NSEntityDescription entityForName:@"Encounter" inManagedObjectContext:self.managedObjectContext];
  [fetchRequest setEntity:entity];
  
  // return distinct
  NSDictionary *entityProperties = [entity propertiesByName];
  [fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:
                                      [entityProperties objectForKey:@"person"], 
                                      [entityProperties objectForKey:@"type"], 
                                      nil]];
  [fetchRequest setReturnsDistinctResults:YES];
  
  [fetchRequest setResultType:NSDictionaryResultType];
  
  NSError *error;
  NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&amp;error];
  
  for (NSDictionary *d in fetchedObjects) {
      NSLog(@"NSDictionary = %@", d);
  }
  
  [fetchRequest release];
  </code></pre>