Note that there are some explanatory texts on larger screens.

plurals
  1. POios: NSFetchRequest and filtering
    text
    copied!<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>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload