How to efficiently get all valid values of an attribute from an NSManagedObject?
I have an iPhone app with a Core Data object that has a "color" attribute. I'd like to get a list of all the values for color that have been saved. A simple SQL statement 开发者_运维知识库SELECT DISTINCT(color) FROM myObjectTable
would easily do this. How can I do this in Core Data without loading all the objects (of which there may be thousands) into an in-memory NSSet?
You can:
1) set NSFetchRequest's requestType to NSDictionaryResultType
2) "setPropertiesToFetch" in NSFetchRequest to fetch only the property instead of the whole object.
I haven't found a good solution to this yet either. But you can as Nevin suggests get specific attributes instead of the entire managed objects.
See Fetching Specific Values from Apple's documentation for more detail.
You will get a NSArray of NSDictionary objects that you can then loop through, extracting the color values that you are looking for.
精彩评论