0

I have a NSFetchRequest (executing in a NSFetchedResultsController) for the following data:

Person (one-to-many) Encounter

Encounter has an int field "type". In my tableView, I want to show:

Person A - Encounter of type 1
Person A - Encounter of type 2
Person B - Encounter of type 1

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.

[EDIT]

Here's the code I'm currently trying. The result is several Encounters with the same type for one Person.

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:&error];

for (NSDictionary *d in fetchedObjects) {
    NSLog(@"NSDictionary = %@", d);
}

[fetchRequest release];
Sofi Software LLC
  • 3,879
  • 1
  • 36
  • 34
  • 1
    Do you mean data validation? Or do you need a specific fetch request? If so, what exactly are you looking for? – Mundi Aug 19 '11 at 23:03

1 Answers1

1

I am assuming you want to return just one record for each type of Encounter even if a Person has more than one of any given type. You can accomplish this by adjusting your NSFetchRequest accordingly:

[fetchRequest setReturnsDistinctValues:YES];
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Actually, with more testing, it does seem to be working in the above code example. I take it that setReturnsDistinctValues means return distinct values for every property specified in setPropertiesToFetch. This is a bit tricky to use with NSFetchedResultsController because the result set is not entities, but NSDictionaries containing the properties I requested... but since Encounter has a pointer to the Person, I can get the fields I need out of that. – Sofi Software LLC Aug 20 '11 at 01:01
  • Okay. There's a few more interesting things to note... the person field in the dictionary is actually an NSManagedObjectID, which can be used to retrieve the Person. It is possible to sort the data such as by Person name using a key value path. Wow, this really works! – Sofi Software LLC Aug 20 '11 at 01:25
  • Using setReturnsDistinctValues means the NSFetchedResultsController can't have a delegate; this makes it pretty useless, so I'll probably get rid of the fetched results controller. See http://stackoverflow.com/questions/3245262/nsfetchedresultscontroller-does-not-support-both-change-tracking-and-fetch-reque. – Sofi Software LLC Aug 22 '11 at 18:31