NSPredicate: Fetch one of each kind

后端 未结 5 2027
眼角桃花
眼角桃花 2021-02-02 11:49

I want to create an NSFetchRequest for objects like this:

The Object is Car which has an attribute color

5条回答
  •  忘掉有多难
    2021-02-02 12:23

    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)

提交回复
热议问题