Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPredicate for searching within relationship entities

I have an entity called Band which has a to-many relationship to a Category entity. The Category entity just contains a categoryName string attribute.

An example record:

Band:       
  bandName: Kiss
  bandCategories:   -  > BandCategory:categoryName:Glam
                    -  > BandCategory:categoryName:Rock

How would I use NSPredicate to search thru all my Bands for bands which match the Rock category, for example?

like image 999
cannyboy Avatar asked Aug 15 '11 10:08

cannyboy


1 Answers

According to the NSPredicate Programming Guide you will need to specify the key path to categoryName with the ANY or ALL specifier.

NSString *category = @"Rock";
[NSPredicate predicateWithFormat:@"ANY bandCategories.categoryName == %@", category];
like image 76
Joe Avatar answered Oct 29 '22 20:10

Joe