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 8448437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:25:37+00:00 2026-06-10T10:25:37+00:00

I am having trouble understanding how to best use Core Data to solve this

  • 0

I am having trouble understanding how to best use Core Data to solve this problem, including the right terminology to describe the problem. Below is a illustrative sample of the problem (but not my actual objects). Assume you have a music playing system, where artists have songs, and every time a song is played in the system, the time stamp is recorded.

Question: How can I find the count of artists with songs played?

Here are sample NSManagedObject

@interface MYArtist : NSManagedObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSSet *songs;
@end

@interface MYSong : NSManagedObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) MYArtist *artist;
@property (nonatomic, retain) NSSet *plays;
@end

@interface MYPlay : NSManagedObject
@property (nonatomic, retain) NSDate *playDate;
@property (nonatomic, retain) MYSong *song;
@end

Below is sample data, for Artists, Songs and Plays, include the play date:

A1 <-+-> S1_A1 <---> P1_S1_A1 (2012-08-31) 
     '-> S2_A1 <-+-> P1_S2_A1 (2012-08-31) 
                 '-> P2_S2_A1 (2012-09-01)
A2 <-+-> S1_A2
     '-> S2_A2 <---> P1_S2_A2 (2012-08-31) 
A3 <---> S1_A3 

Using the code below, I can fetch all the MYPlay objects and build a set of artists and then find the size of the set at the end. The resulting set from this sample data and code would be [A1, A2] with count = 2. However, I would expect some NSPredicate syntax or use of countForFetchRequest to be more efficient, instead of iterating over the objects and faulting them into memory.

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"MYPlay"];
NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

NSMutableSet *set = [[NSMutableSet alloc] init];
for (id result in results) {
    MYPlay *play = (MYPlay *)result;
    [set addObject:play.song.artist];
}

NSUInteger count = set.count;
  • 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-10T10:25:39+00:00Added an answer on June 10, 2026 at 10:25 am

    The poorly documented SUBQUERY might be a good solution, and you can nest them so you can check associations of associations.

    So:

    NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:@"MYArtist"]; 
    request.predicate = [NSPredicate predicateWithFormat:@"(SUBQUERY(songs,$song,SUBQUERY($song.plays,$play,$play.playDate NOT NULL).@count > 0).@count >0)"]; // I'm assuming that songs with out a play have a nil playDate
    
    NSUInteger count = [context countForFetchRequest:request error:&error];
    

    I haven’t tested that subquery string, but that should get you started. There’s a few good resources out there if you google for them, but not much in the Apple docs. Documentation for NSExpression states the “string format for a subquery expression is:”

    SUBQUERY(collection_expression, variable_expression, predicate);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: I'm having some trouble understanding exactly how to best use controllers in an
I'm having trouble understanding how to solve this issue, essentially I have two arrays
I'm having trouble understanding the top level of abstraction of this problem. The Problem:
I'm having some trouble getting my head around how best to solve this situation
I am having trouble understanding this code: static long long _be_decode_int(const char **data, long
Im having trouble understanding this : why Microsoft put Xattribute under Xobject ? they
I am having trouble understanding the foursquare Terms of Use concerning the Venues API.
I'm having trouble understanding why I would use a context.xml file to declare a
Im having trouble understanding this class and everything ive seen hasn't really helped. Ill
I am having trouble understanding this java code. I want the image to twinkle

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.