因此,我正在构建一个小应用程序,它使用具有4个实体的〜25mb大小的核心数据库。这是公交时刻表。Core data setReturnsDistinctResult not working
在一个名为“Stop”的表中,有~1300个具有属性“name”,“id”,“longitude”,“latitude”和夫妻关系的巴士站条目。现在有很多站点具有相同的名称属性,但坐标和ID不同。所以我想在表格视图中显示所有截然不同的名称,我使用setReturnsDistinctResults和NSDictionaryResultType和setPropertiesToFetch。但setReturnsDistinctResult不起作用,我仍然得到所有条目。
继承人代码:
- (NSFetchRequest *)fetchRequest {
if (fetchRequest == nil) {
fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Stop" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSArray *sortDescriptors = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES] autorelease]];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"name"]]];
[fetchRequest setReturnsDistinctResults:YES];
DebugLog(@"fetchRequest initialized");
}
return fetchRequest;
}
/////////////////////
- (NSFetchedResultsController *)fetchedResultsController {
if (self.predicateString != nil) {
self.predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", self.predicateString];
[self.fetchRequest setPredicate:predicate];
} else {
self.predicate = nil;
[self.fetchRequest setPredicate:predicate];
}
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:self.fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:sectionNameKeyPath cacheName:nil];
return fetchedResultsController;
}
//////////////
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [[fetchedResultsController objectAtIndexPath:indexPath] valueForKey:@"name"];
return cell;
}
还没有找到解决方案。也许这是一个错误,因为name属性中的字符串不仅使用拉丁字母:\。接着使用executeFetchRequest并过滤数组中的所有条目。 – Moze 2010-06-21 08:41:07