How to fetch distinct values in Core Data?
- by Andy
So in looking through Core Data Snippets, I found the following code:
...
[request setEntity:entity];
[request setResultType:NSDictionaryResultType];
[request setReturnsDistinctValues:YES];
[request setPropertiesToFetch:[NSArray arrayWithObject:@"<#Attribute name#>"]];
// Execute the fetch
NSError *error;
id requestedValue = nil; // WTF? This isn't defined or used anywhere
NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];
if (objects == nil) {
// handle the error
}
This is great and seems perfect for what I need...but how does one actually use it? I assume since it's returning dictionaries, I need a key to get at the values - but where's the key defined? Is that the "id requestedValue = nil" line? If so, how does "requestedValue" become the key? Xcode gives me a compiler warning about an unused variable at the "requestedValue" declaration. I feel like I'm missing something here.
Thanks in advance for any assistance you can offer.