I was wondering if there was a predicate to return every unique instance in a column.
A:
@distinctUnionOfArrays
might work, depending on your situation: http://stackoverflow.com/questions/1996279/core-data-getting-unique-rows
dontGoPlastic
2010-08-19 20:21:42
A:
You can configure the NSFetchRequest
to return uniques for you. Take a look at its documentation.
Marcus S. Zarra
2010-08-19 22:00:25
That is originally what I attempted to use, setReturnsDistinctResults which uses propertiesToFetch. Problem is, I want to use a NSFetchedResultsController; not a NSDictionaryResultType, which is required, for the results.
Oh Danny Boy
2010-08-23 13:17:52
You cannot return uniques and use the `NSFetchedResultsController` together. That is not the intended design. What is your ultimate goal?
Marcus S. Zarra
2010-08-23 17:12:40
I have a list of locations in a sqlite db. They have neighborhoods. I want the user to be able to search by neighborhoods. The neighborhoods populate a tableview. Problem is, I have dozens of each neighborhood. I just want one unique instance of each neighborhood.
Oh Danny Boy
2010-08-24 15:58:40
Why don't you have relationships between locations and neighborhoods so that each location has one neighborhood and a neighborhood has many locations? Sounds like a data model issue.
Marcus S. Zarra
2010-08-24 19:13:36
I ditched the NSFetchedResultsController and stored the data in an array, then made an NSSet of the array, then transferred the data back to the array. This seemed to work. Thank you for the help.
Oh Danny Boy
2010-08-25 19:36:58
A:
Below is the code used to solve the problem.
- (NSMutableArray *)fetchNeighborhoods {
ProjectNameAppDelegate *appDelegate = (ProjectNameAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Bar" inManagedObjectContext:managedObjectContext];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"neighborhood" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setEntity:entity];
// Get array of results.
NSMutableArray *theResults = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
// Grab unique neighborhoods through NSSet.
NSSet *uniqueElements = [NSSet setWithArray:[theResults valueForKey:@"neighborhood"]];
// Dump NSSet uniques into new array.
NSMutableArray *sortedResults = [[NSMutableArray alloc] initWithArray:[uniqueElements allObjects]];
// Sort the results alphabetically.
sortedResults = [[sortedResults sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] mutableCopy];
// Output unique results for verification.
for(int i = 0; i < [sortedResults count];i++){
NSLog(@"%d. %@", i+1, [sortedResults objectAtIndex:i]);
}
[sortDescriptor release];
[sortDescriptors release];
return sortedResults;
}
Oh Danny Boy
2010-08-26 14:15:50