我正试图从NSPredicate获得一个独特的结果。
我的代码:
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];
问题是结果会在同一个城市返回很多次。该实体有很多会员,每个会员都有一个属性“city”。
我做错了什么?
谢谢,
RL
答案 0 :(得分:7)
务必将resultType
的{{1}}设置为NSFetchRequest
。默认设置是返回实际对象,因此它将忽略NSDictionaryResultType
。
答案 1 :(得分:6)
在@Alex的帮助下,这里是最终代码,没有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];
这将返回带有城市列表的NSArray。
感谢。
答案 2 :(得分:3)
来自官方文件:
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.
}