イベント タイトルが重複している Core Data にロードされたイベント データベースがあります。これは、データベースがイベントの日ごとに固有の情報を提供できるようにするために作成されました。たとえば、日付ごとの価格変動。
フィルターを提供するために、NSFetchRequest と NSPredicate を使用してテーブル ビューとして表示されるリストから、重複したイベント タイトルを削除する必要があります。しかし、私が見たすべての例では、述語フィルターのターゲットとして使用するために動的キー値を使用する必要がありません。たとえば、以下の NSDate は現在時刻をキー フィルターとして提供し、機能します。
現在、NSString * title は、nil 値を返すイベント 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;
}