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
)
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With