核心数据集ReturnsDistinctResult无法正常工作

4
我正在开发一个小应用程序,它使用大小约为25MB的核心数据数据库,其中包含4个实体。这是用于公交车时间表的。
在名为“Stop”的一个表中,有大约1300个巴士站点记录,其中包含属性“名称”、“ID”、“经度”、“纬度”和几个关系。现在有许多具有相同名称属性但不同坐标和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
1个回答

4

对于你来说可能有些晚,但可以帮助其他人。

我也遇到了同样的问题。我发现,如果使用持久化存储类型NSSQLiteStoreType,则返回不同的结果是有效的。但是对于NSXMLStoreType,不同的值无效。

我还没有测试过NSBinaryStoreType和NSInMemoryStoreType的结果。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接