Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 466
Rui Lopes Avatar asked Oct 08 '11 18:10

Rui Lopes


People also ask

How do I use nsfetchrequest in Java?

You use NSFetchRequest objects with the methods fetch (_:) and count (for:), defined by NSManagedObjectContext. You often predefine fetch requests in a managed object model— NSManagedObjectModel provides an API to retrieve a stored fetch request by name.

What is the return type of executefetchrequest?

The executeFetchRequest:error: method has two possible results. It either returns an NSArray object with zero or more objects, or it returns nil. If nil is returned, you have received an error from Core Data and need to respond to it.

What is fetch request in Salesforce?

A fetch request that retrieves results asynchronously and supports progress notification. A fetch result object that encompasses the response from an executed asynchronous fetch request. A controller that you use to manage the results of a Core Data fetch request and to display data to the user.

How to display distinct values for SSRs parameter when query returns duplicates?

Distinct values for SSRS Parameter when query returns Duplicates. Step 1 : Create a report and Go to the Report Tab at the top. Step 2 : Go to the Report properties and then code and write the following code in it. Step 3 : Create a Dataset which contain all the values that need to be display in the ...


2 Answers

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.

like image 146
Alex Avatar answered Sep 21 '22 15:09

Alex


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.

like image 33
Rui Lopes Avatar answered Sep 18 '22 15:09

Rui Lopes