views:

131

answers:

2

I've got some data stored in Core Data that looks something like:

| name | identifier | other_stuff |

I need to display the names in a UITableView, but I only want to display the names with unique name-identifier pairs. So for:

John | 3 | foo
Betty | 4 | foo
Betty | 4 | bar

I only want the query to return John, Betty. Something like "select unique name, identifier from table."

Any way to do this with NSPredicate, or do I need to pour the de-duped fields into a different container, then search on that?

+2  A: 

Since this is Core Data, check out -[NSFetchRequest setReturnsDistinctObjects:].

Dave DeLong
A: 

Core Data is an object graph management framework that just happens to (optionally) persist that object graph to a SQL persistent store. There is no way to query object attributes, just objects. You can query the instances then, from the resulting array:

NSArray *instances; //from -[NSManagedObject executeFetchRequest:error:]
NSArray *uniqueNames = [instances valueForKeyPath:@"@distinctUnionOfObjects.name"];

assuming the name is in the name property.

If what you want is for each user to have one name, one id and multiple {foo,bar,...}, then you should model the situation as a user entity with name and id and a to-many relationship to an entity that represents foo/bar/etc.

Barry Wark