Can you specify "select a unique name from ..." using NSPredicate?

I have data stored in Core Data that looks something like this:

| name | identifier | other_stuff |

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

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

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

Any way to do this using NSPredicate, or do I need to pour deactivated fields into another container and then search on it?

+6
iphone cocoa-touch cocoa core-data nspredicate
source share
2 answers

Core Data is an object graph management structure that simply happens (optionally) by storing this object graph in SQL persistent storage. It is not possible to request attributes of objects, just objects. Then you can request instances from the resulting array:

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

provided that the name is in the name property.

If you want each user to have one name, one id and several {foo, bar, ...}, you should model the situation as a user object with a name and id and a to-many relation to the entity that represents foo / bar / etc.

For more information about @distinctUnionOfObjects (and other collection operations), see the section “Collection Operators” in the “Key Value Encoding Guide” section.

+10
source share

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

+9
source share