1
votes

I am having trouble writing a fetch request that will fetch data objects based on the contents of a to-many relationship. One table is for photos and the other is for tags. Each photo can have multiple tags and each tag can have multiple photos.

So, the data model is: Photos <<-->> Tags. Photo has an attribute called 'title' and a relationship called 'tags'. Tag has an attribute called 'label' and a relationship called 'taggedPhotos'. I have created subclasses for both object types. (I'm working on the Stanford CS193p course).

I want a fetch request predicate for the Photo entity that will select photo objects if their 'tags' relationship contains a given tag label. So if I have a label called 'architecture' I want a match with any photos that have 'architecture' as one of their tags.

1

1 Answers

4
votes

If I am understanding you correct I think the predicate should be like:

NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"ANY tags.label LIKE %@", @"searchKey""];

and for sure it's a request on the photo entity.

Here is the Apple NSPreciate Documentation for further information