Jim*_*Jim 5 sorting iphone filtering predicate core-data
我有一个事件数据库加载到核心数据,具有重复的事件标题.这样做可以使数据库为事件的每一天提供独特的信息.例如每个日期的定价波动.
我现在需要从列表中删除重复的事件标题,该列表将使用NSFetchRequest和NSPredicate显示为表视图以提供过滤器.但是我看到的所有示例都要求使用无动态键值作为谓词过滤器的目标.例如,NSDate现在提供时间作为关键过滤器并且它可以工作.
目前,NSString*title定位事件ManagedObject类中返回nil值的值.这是来自FetchResultsController的剪辑.
- (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;
}
Run Code Online (Sandbox Code Playgroud)
你可以设置
setReturnsDistinctResults:YES
Run Code Online (Sandbox Code Playgroud)
在你的 fetchRequest 上。
有关更多信息,请参阅文档: NSFetchRequest 类参考
| 归档时间: |
|
| 查看次数: |
4903 次 |
| 最近记录: |