Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8755357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:50:26+00:00 2026-06-13T13:50:26+00:00

Suppose I have a Song entity and Genre entity in Core Data. Genres have

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:50:26+00:00Added an answer on June 13, 2026 at 1:50 pm

    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:

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY songs != NULL"];
    NSEntityDescription *genres = [NSEntityDescription entityForName:@"Genre" 
                                              inManagedObjectContext:moc];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:genres];
    [request setPredicate:predicate];
    NSError *error;
    NSArray *genresArray = [moc executeFetchRequest:request error:&error];
    NSLog(@"Total number of genres that have songs: %d", genresArray.count);
    for (NSManagedObject *genre in genresArray) {
      NSLog(@"Genre: %@ (%@)", [genre valueForKey:@"genreName"], [genre valueForKeyPath:@"songs.@count"]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have many C functions of the same signatures and we want to
I have these tables Genre and Songs . There is obviously many to many
Suppose I have two h:inputText components. I want to bind both of the text
Suppose Song s have_many Comment s; How can I: Pull a list of all
Suppose I have 3 language: en , tc , sc , I want to
Suppose if we have the album, artist, and song model in rails. In album:
Suppose we have the following: data Foo x from_list :: [x] -> Foo x
Suppose I have a data frame, df, that looks like: f t1 t2 t3
Suppose we have the Lyric model: class Lyric < ActiveRecord::Base belongs_to :song end and
Suppose I have a bunch of static fields and I want to use them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.