Iphone 使用谓词对核心数据进行排序以消除重复项
我有一个事件数据库加载到核心数据中,其中包含重复的事件标题。这使得数据库能够为活动的每一天提供唯一的信息。例如每个日期的价格波动 我现在需要从列表中删除重复的事件标题,该列表将显示为带有NSFetchRequest和NSPreditate的表视图,以提供筛选器。但是,我看到的所有示例都要求使用非动态键值作为谓词过滤器的目标。下面的示例NSDate将现在的时间作为一个关键过滤器提供,它可以工作 当前,NSString*title以events ManagedObject类中返回nil值的值为目标。下面是FetchResultsController的剪报Iphone 使用谓词对核心数据进行排序以消除重复项,iphone,core-data,sorting,filtering,predicate,Iphone,Core Data,Sorting,Filtering,Predicate,我有一个事件数据库加载到核心数据中,其中包含重复的事件标题。这使得数据库能够为活动的每一天提供唯一的信息。例如每个日期的价格波动 我现在需要从列表中删除重复的事件标题,该列表将显示为带有NSFetchRequest和NSPreditate的表视图,以提供筛选器。但是,我看到的所有示例都要求使用非动态键值作为谓词过滤器的目标。下面的示例NSDate将现在的时间作为一个关键过滤器提供,它可以工作 当前,NSString*title以events ManagedObject类中返回nil值的值为目标。
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController == nil) {
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
NSPredicate *predicate = [[[NSPredicate alloc] init] autorelease];
[fetchRequest setReturnsObjectsAsFaults:NO];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext]];
NSArray *sortDescriptors = nil;
NSString *sectionNameKeyPath = nil;
NSDate *date = [NSDate date];
NSString *title = [events title];
if ([fetchSectioningControl selectedSegmentIndex] == 1) {
predicate = [NSPredicate predicateWithFormat:@"(closeDate >= %@) AND (title == %@)", date, title ];
sortDescriptors = [NSArray arrayWithObjects:[[[NSSortDescriptor alloc] initWithKey:@"category.name" ascending:YES] autorelease], [[[NSSortDescriptor alloc] initWithKey:@"openDate" ascending:YES] autorelease], nil];
sectionNameKeyPath = @"category.name";
} else if ([fetchSectioningControl selectedSegmentIndex] == 0){
predicate = [NSPredicate predicateWithFormat:@"closeDate >= %@", date];
sortDescriptors = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"openDate" ascending:YES selector:@selector(compare:)] autorelease]];
sectionNameKeyPath = @"day";
}
[fetchRequest setPredicate:predicate];
[fetchRequest setSortDescriptors:sortDescriptors];
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:sectionNameKeyPath cacheName:@"EventsCache"];
}
return fetchedResultsController;
}
你可以设置
setReturnsDistinctResults:YES
根据你的要求
有关更多信息,请参阅文档:
感谢您的反馈。不幸的是,在这个阶段,它提供的问题多于解决方案。据我所知,结果需要作为NSDictionary返回,因此我失去了它们作为托管对象返回的一些功能。也就是说,数据还与另一个名为“类别”的实体有关系,我以前没有提到过,即同位语。它定义了每个事件的各个部分。