Core data many-to-many relationship - Predicate question

This seems to work OK:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY lists == %@)", myList];

Where myList is an actual List entity.


Given a data model like:

List <<——>> Patient,

you can find all Patient instances that belong to a list with a particular name with a fetch request on the Patient entity using a predicate like:

[NSPredicate predicateWithFormat:@"ANY lists.name LIKE[cd] %@", listName]

assuming listName is an NSString instance with the list name you want. LIKE[cd] does a case-insensitive and diacritic-insensitive comparison.


It sounds like your data model is this:

List <<-->> Patient

I would think that if you know the particular list name, then you know the particular list object. If so, you can just grab the patients using the to-many relationship from List to Patient--it is a set of patient objects. For example, if the relationship from List to Patient is named "patients":

NSSet *patientSet = listObject.patients;

Note: this requires that you create subclasses for your managed objects so you can access the attributes and relationships as properties on your objects.

If you only know the list name for some reason, and you are fetching Patient objects, then you can create a predicate using the to-many relationship from Patient to List (assume it's named "lists" and the list's name in a string named "listName"):

NSPredicate *pred = [NSPredicate predicateWithFormat:@"ANY lists.name == %@",listName];