核心数据集ReturnsDistinctResult 不起作用

编程入门 行业动态 更新时间:2024-10-24 03:25:38
本文介绍了核心数据集ReturnsDistinctResult 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

So i'm building a small application, it uses core data database of ~25mb size with 4 entities. It's for bus timetables.

在一个名为Stop"的表中,有大约 1300 个公交车站条目,具有属性name"、id"、longitude"、latitude"和夫妻关系.现在有许多具有相同 name 属性但不同坐标和 id 的站点.所以我想在表视图中显示所有不同的停止名称,我使用 setReturnsDistinctResults 和 NSDictionaryResultType 和 setPropertiesToFetch.但是 setReturnsDistinctResult 不起作用,我仍在获取所有条目.

In one table named "Stop" there are ~1300 entries of bus stops with atributes "name", "id", "longitude", "latitude" and couple relationships. Now there are many stops with identical name attribute but different coordinates and id. So I want to show all distinct stop names in table view, i'm using setReturnsDistinctResults with NSDictionaryResultType and setPropertiesToFetch. But setReturnsDistinctResult is not working and I'm still getting all entries.

这里的代码:

- (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; }

推荐答案

这对你来说可能有点晚,但可能会帮助其他人.

This may be late for you, but may help others.

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

I had the same problem. What I found is that, if persistant store type is NSSQLiteStoreType the returnDistinctResults works. But for NSXMLStoreType distinct values are not working.

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

I haven't tested results for NSBinaryStoreType and NSInMemoryStoreType.

更多推荐

核心数据集ReturnsDistinctResult 不起作用

本文发布于:2023-11-16 10:11:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603348.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   核心   数据   ReturnsDistinctResult

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!