Get unique objects at the end of a relationship to get results

In my application, I have two entities - page and annotation. In my Core Data model they have an attitude of "one to many": Page <-->> Annotation

.

I am retrieving the Annotation object because I need to apply certain predicates, but ultimately you want a unique set of annotation-related pages that match the predicate.

How can I get the set of unique page objects that are relevant to the annotations returned from the selection? Can you do it in one choice? I tried some things with NSDictionaryResultType

but got errors and ultimately, I want objects, not values.

+3


source to share


1 answer


You don't want to use NSDictionaryResultType

as it doesn't return objects. What you want to do is use KVC after you've retrieved the instances Annotation

like this:

NSArray *results = ...; //Your fetch of Annotation objects
NSArray *uniquePages = [results valueForKeyPath:@"@distinctUnionOfObjects.page"];

      



The array uniquePages

will contain your unique collection of instances Page

.

+7


source







All Articles