Note that there are some explanatory texts on larger screens.

plurals
  1. POSorting Core Data with Predicate to eliminate duplicates
    primarykey
    data
    text
    <p>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. </p> <p>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.</p> <p>Currently NSString * title targets a value in the events ManagedObject class that returns a nil value. Here is a snip from the FetchResultsController.</p> <pre><code>- (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 &gt;= %@) 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 &gt;= %@", 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload