views:

495

answers:

2

I have an Event database loaded into Core Data that has duplicate Event titles. This has been made so the database can provide unique information for each day of the event. Eg fluctuations in pricing on each date.

I now need to remove the duplicate event titles from a list that will be displayed as table view with NSFetchRequest and NSPredicate to provide the filter. But all the examples I've seen require a none dynamic key value to be used as a target for the predicate filter. eg below NSDate provides the time now as a key filter and it works.

Currently NSString * title targets a value in the events ManagedObject class that returns a nil value. Here is a snip from the 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;
}
+1  A: 

You could set

setReturnsDistinctResults:YES

on your fetchRequest.

For more, see the docs: NSFetchRequest Class Reference

weichsel
Thanks for your feedback. Unfortunately it's provided more problems than solutions at this stage. The results as far as I can tell need to be returned as a NSDictionary so I loose some of the functionality of them returned as managed objects. ie. The data has also a relationship to another entity called 'Category' which I didn't mention before, appologies. It defines the sections of each event.
Jim
+1  A: 

Same problem here. setReturnsDistinctResults:YES works with the use of NSDictionaryResultType but I need managedObjects. I end up with Duplicates. I spent hours on this. Does anyone has a solution?

Vincent Gaulet