2
votes

enter image description here

Above is my Core Data relationships. This is a cocoa app with three NSTableView showing Genre, Album and Song. I want to filter GenreArrayController such that the Genre TableView only shows Genre name where Albums - Songs length is > 4.

When I Use Predicate on SongArrayController:

[_songArrayController setFilterPredicate:[NSPredicate predicateWithFormat:@"length > 4"]];

The result is Genre and Album remain unfiltered but Songs table do get filtered. Is is possible to reach Genre - Album - Songs using NSPredicate on Genre.

Am I doing this wrong way, can this be achieved by altering the Entities and their relations.

2
I have not much experience with predicates, but you likely need to set the filter on the Genre ArrayController, and set something like "ALL albums.songs.length > 4". See for predicate syntax: developer.apple.com/library/mac/documentation/Cocoa/Conceptual/…fishinear

2 Answers

1
votes

You can combine predicates with && or by using the more wordy compound predicates. You can access the key paths of the relationships in your predicate via dot notation. Thus, to filter songs by genre:

NSPredicate(format: "length > 4 && album.genre = %@", genreObject)

or even

NSPredicate(format: "length > 4 && album.genre.name = %@", "Jazz")
1
votes

Using subquery, I am able to filter Genre such that Genre table only shows genre, whose album contains numbers of songs greater than 4:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(albums, $x, ANY $x.songs.count > 4).@count != 0"];