gpt4 book ai didi

iphone - 核心数据集ReturnsDistinctResult不起作用

转载 作者:行者123 更新时间:2023-12-03 19:12:43 32 4
gpt4 key购买 nike

所以我正在构建一个小型应用程序,它使用大约 25mb 大小的核心数据数据库和 4 个实体。这是公交车时刻表。

在一个名为“Stop”的表中,有大约 1300 个公交车站条目,其属性包括“名称”、“id”、“经度”、“纬度”和情侣关系。现在有许多站点具有相同的名称属性,但坐标和 ID 不同。所以我想在 TableView 中显示所有不同的停靠点名称,我将 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;
}

最佳答案

这对您来说可能已经晚了,但可能对其他人有帮助。

我也遇到了同样的问题。我发现,如果持久存储类型是 NSSQLiteStoreType,则 returnDistinctResults 有效。但对于 NSXMLStoreType 不同的值不起作用。

我尚未测试 NSBinaryStoreType 和 NSInMemoryStoreType 的结果。

关于iphone - 核心数据集ReturnsDistinctResult不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2834282/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com