Rui Lopes
Rui Lopes

Reputation: 2562

NSFetchRequest with distinct properties

I'm trying to get a distinct result from NSPredicate.

My code:

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Members" inManagedObjectContext:context];
    request.entity = entity;
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"city"
                                                                                     ascending:YES
                                                                                      selector:@selector(caseInsensitiveCompare:)]];

    request.predicate = [NSPredicate predicateWithFormat:@"memberDeleted == %@", [NSNumber numberWithBool:NO]];

    NSDictionary *properties = [entity propertiesByName];
    request.propertiesToFetch = [NSArray arrayWithObject:[properties objectForKey:@"city"]];
    request.returnsDistinctResults = YES;

    request.fetchBatchSize = 20;

    NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                          managedObjectContext:context
                                                                            sectionNameKeyPath:nil
                                                                                     cacheName:@"CityCache"];
    [request release];

    self.fetchedResultsController = frc;
    [frc release];

The issue is that the result returns many times the same City. This Entity has a lot of Members, and each Member has a property "city".

What am I doing wrong?

Thanks,

RL

Upvotes: 5

Views: 7958

Answers (3)

xster
xster

Reputation: 6677

From the official documentation:

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 :[NSArray arrayWithObject:@"<#Attribute name#>"]];

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

Upvotes: 3

Rui Lopes
Rui Lopes

Reputation: 2562

With the help of @Alex, here's the final code, without FRC:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Members" inManagedObjectContext:self.managedObjectContext];
request.entity = entity;
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"city"
                                                                                 ascending:YES
                                                                                  selector:@selector(caseInsensitiveCompare:)]];

request.predicate = [NSPredicate predicateWithFormat:@"memberDeleted == %@", [NSNumber numberWithBool:NO]];

NSDictionary *properties = [entity propertiesByName];
request.propertiesToFetch = [NSArray arrayWithObject:[properties objectForKey:@"city"]];
request.returnsDistinctResults = YES;
request.resultType = NSDictionaryResultType;
request.fetchBatchSize = 20;

NSError *error = nil;
NSArray *tempArray = [self.managedObjectContext executeFetchRequest:request error:&error];

NSMutableArray *cities = [[NSMutableArray alloc] init];
for (int i=0; i < [tempArray count]; i++){
    NSDictionary *tempDict = [NSDictionary dictionary];
    tempDict = [tempArray objectAtIndex:i];

    if ([tempDict objectForKey:@"city"] != nil)
        [cities addObject:[tempDict objectForKey:@"city"]];
}

//if the tempArray has no nil values, it's more efficient with:
//NSArray* cities = [tempArray valueForKeyPath:@"city"];

self.cityArray = cities;
[cities release];
[request release];

This returns a NSArray with the list of the Cities.

Thanks.

Upvotes: 6

Alex
Alex

Reputation: 26859

Make sure to set the resultType of the NSFetchRequest to NSDictionaryResultType. The default is to return the actual objects, and so it will ignore propertiesToFetch.

Upvotes: 7

Related Questions