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
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.
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.
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.
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 ...
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With