开发者

NSFetchRequest with distinct properties

开发者 https://www.devze.com 2023-04-11 12:08 出处:网络
I\'m trying to get a distinct result from NSPredicate. My code: NSFetchRequest *request = [[NSFetchRequest alloc] init];

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 propertiesByNa开发者_运维技巧me];
    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


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.


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.
}
0

精彩评论

暂无评论...
验证码 换一张
取 消