NSPredicate: Fetch one of each kind

arnoapp picture arnoapp · May 7, 2013 · Viewed 10.4k times · Source

I want to create an NSFetchRequest for objects like this:

The Object is Car which has an attribute color. I have four cars:

car1.color = red
car2.color = red
car3.color = blue
car4.color = green

I want to create an NSPredicate that selects only one car for each color(it doesn't matter which car it selects.

How can I achieve this?

In fact I'm looking for something similar like a DISTINCT in SQL

Answer

nielsbot picture nielsbot · Jul 12, 2013

Core Data does support fetching distinct values. Apple even provides sample code here: http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/CoreDataSnippets/Articles/fetchExpressions.html


From that page:

Fetching Distinct Values

To fetch the unique values of a particular attribute across all instances of a given entity, you configure a fetch request with the method setReturnsDistinctResults: (and pass YES as the parameter). You also specify that the fetch should return dictionaries rather than managed objects, and the name of the property you want to fetch.

Swift 2.0

let context:NSManagedObjectContext! = <#Get the context#>
let entity = NSEntityDescription.entityForName( "<#Entity name#>",  inManagedObjectContext:context )
let request = NSFetchRequest()
request.entity = entity
request.resultType = .DictionaryResultType
request.returnsDistinctResults = true
request.propertiesToFetch = [ "<#Attribute name#>" ]

var objects:[[String:AnyObject]]?
do
{
    try objects = context.executeFetchRequest( request )
}
catch
{
    // handle failed fetch
}

Obj-C

NSManagedObjectContext *context = <#Get the context#>;  

NSEntityDescription *entity = [NSEntityDescription  entityForName:@"<#Entity name#>" inManagedObjectContext:context];  
NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
[request setEntity:entity]; 
[request setResultType:NSDictionaryResultType];
[request setReturnsDistinctResults:YES]; 
[request setPropertiesToFetch:@[@"<#Attribute name#>"]];  

// Execute the fetch. 

NSError *error = nil; 
id requestedValue = nil; 
NSArray *objects = [context executeFetchRequest:request error:&error]; 
if (objects == nil) {
    // Handle the error. 
} 

(One "gotcha" is that the results will be returned as dictionaries--if you need the objects you can do another fetch to get them by ID)