Suppose I have a Song entity and Genre entity in Core Data. Genres have many Songs.
I want to perform a fetch request to fetch all songs’ genres. This implies genres would show up multiple times. Or, in SQL terms,
select G.*
from Songs S
join Genres G on G.id = S.genre_id
I realise the SQL equivalent is a bit of a stretch since I don’t need foreign/primary keys. The goal is the same. It seems simple, but I couldn’t find an answer in my research that quite fit my problem.
My ultimate goal is to aggregate genres and count them, like in this post. Again, in SQL terms:
select G.name, count(*)
from Songs S
join Genres G on G.id = S.genre_id
group by G.name
I’m hoping that with the understanding from this question, I can add the code from the link to get what I want. Feel free to tell me it won’t work if CD just doesn’t work that way.
Assuming that in your core data model each Genre has a to-many relationship with a Song, the following will give you a count of songs for each genre (which is actually aggregating your genres and counting them), where the genre is attached to at least 1 song: