Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core data many-to-many relationship - Predicate question

Tags:

In my Core Data model I have two entities: List and Patient. List has an attribute called 'name'.

A List can have any number of Patients and each Patient can belong to any number of different lists. I have therefore set a relationship on List called 'patients' that has an inverse to-many relationship to Patient AND a relationship on Patient called 'lists' that has a to-many relationship to List.

What I'm struggling to figure out is how to create a Predicate that will select all Patients that belong to a particular List name.

How would I go about this? I have never used relationships before in Core Data.

like image 464
Garry Pettet Avatar asked Feb 24 '10 18:02

Garry Pettet


2 Answers

This seems to work OK:

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

Where myList is an actual List entity.

like image 63
Garry Pettet Avatar answered Oct 13 '22 12:10

Garry Pettet


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.

like image 34
Barry Wark Avatar answered Oct 13 '22 14:10

Barry Wark