Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"multiple to-many keys not allowed here" issue with a predicate

I need some help with a predicate for a NSFetchedResultsController. My guess is that can be done with a SUBQUERY but I can not figure out how.

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Album"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"ANY tracks.playlistItems.playlist == %@", section.playlist];

When executing a fetch request with this predicate I get a “multiple to-many keys not allowed here” error. Which makes sense since album has multiple tracks, and a track has multiple playlist items.

So using natural language here is what I want from this predicate: an album where at least one of its tracks has at least one of its playlistItems that has the following playlist.

Can I achieve this using a SUBQUERY in my predicate?

(I know that this can be easily achieved using 2 fetch requests, but I was wondering if this can be done using one request, since I am using a NSFetchedResultsController)

like image 676
dariaa Avatar asked Jul 02 '14 14:07

dariaa


1 Answers

Yes, you can do this using a SUBQUERY. I have only ever used subqueries a couple of times, but I think yours should look like this:

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(tracks, $t, ANY $t.playlistItems.playlist == %@).@count != 0", section.playlist];
like image 160
sfeuerstein Avatar answered Oct 13 '22 17:10

sfeuerstein