我有一個事件數據庫加載到具有重複事件標題的核心數據中。這樣做是爲了使數據庫能夠爲事件的每一天提供獨特的信息。例如每個日期的價格波動。使用謂詞對核心數據進行排序以消除重複項
我現在需要從列表中刪除重複的事件標題,該列表將顯示爲NSFetchRequest和NSPredicate的表視圖以提供過濾器。但是我所見過的所有示例都需要一個無動態鍵值才能用作謂詞過濾器的目標。例如NSDate下面提供現在的時間作爲關鍵過濾器,它的工作原理。
當前NSString *標題的目標是事件ManagedObject類中返回一個零值的值。這是來自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;
}
同樣的問題:
如需更多信息,請參閱該文檔。 – 2010-12-03 16:48:03