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

  • SEARCH
  • Home
  • 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 8652543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:19:50+00:00 2026-06-12T14:19:50+00:00

I asked a question yesterday where I really should have started with a simpler

  • 0

I asked a question yesterday where I really should have started with a simpler example. Having distilled my question to the basics, I’ve managed to solve my problem using existing SO questions and answers.

I’m summarising my question here (and providing my own solution) because I don’t think there are any posts that explain this clearly enough. Being new to Core Data and struggling to get away from SQL concepts, I’d welcome feedback on how appropriate my solution is, and if there are better ways of modelling the problem.

Question

Given the following object model, that has three entities A, B and C, each linked by to-many relationships:

Model

How is it possible to identify the parent A entities, that have grandchildren C entities with a particular attribute? By example, using these sample entities and their relationships:

example

How can I find our which entity A s have child entity C s with the Tag:Yes?

Solution

I’ve been able to achieve this is using the SUBQUERY keyword of NSPredicate. Here is the code snippet that worked for me (assuming you’ve set up your managed object context etc):

NSError *error = nil;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"EntityA" inManagedObjectContext:context];   
[fetchRequest setEntity:entity];
NSPredicate *predicateTemplate = [NSPredicate predicateWithFormat:@"(0 != SUBQUERY(child, $a, (0 != SUBQUERY($a.child, $child, $child.tag == %@).@count)).@count)", @"YES"]; 
[fetchRequest setPredicate:predicateTemplate];
NSArray *test = [context executeFetchRequest:fetchRequest error:&error];

Breaking the all important NSPredicate string onto several lines:

(0 != SUBQUERY(child, 
               $a, 
               (0 != SUBQUERY($a.child,
                              $child, 
                              $child.tag == %@).@count)
               ).@count
)

The important part is we are selecting the EntityA to work from and then in the nested subquery working through the child relationships of the entities. I expect this can be repeated for several depths. Not the most intuitive thing to put together… but it works. Comments welcome.

  • 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-12T14:19:51+00:00Added an answer on June 12, 2026 at 2:19 pm

    I’ve never tried to use a subquery before, that’s very cool and good to see an example of it. You should add the solution as an answer to the question actually!

    I would have probably done as Abizern suggested, since this is a tree-like hierarchy and its easier to traverse up the tree using to-one relationships.

    In code this looks like:

    NSManagedObjectContext *moc = APPDELEGATE.managedObjectContext;
    NSFetchRequest *request = [NSFetchRequest new];
    [request setEntity:[NSEntityDescription entityForName:@"EntityC" inManagedObjectContext:moc]];
    [request setPredicate:[NSPredicate predicateWithFormat:@"tag = YES"]];
    NSError *fetchError = nil;
    NSArray *children = [moc executeFetchRequest:request error:&fetchError];
    

    children is an array of EntityC objects that match the predicate. The next step is getting a set of unique EntityA objects that are the “grandparents” of these. We can take advantage of key-value coding here:

    NSArray *grandParents = [children valueForKeyPath:@"parent.@distinctUnionOfObjects.parent"];
    

    In this case, for efficiency, we’d probably want to prefetch the parent.parent keypath during our initial fetch request:

    [request setRelationshipKeyPathsForPrefetching:@[@"parent.parent"]];
    

    Hope this helps!

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

Sidebar

Related Questions

I asked a similar question yesterday, but have acquired a really odd problem since
I asked a question yesterday about a problem that Im having with a program
this is really strange, yesterday I asked the question Yesterdays question how to implement
i asked this question yesterday but it doesnt seem to have asked properly so
I have a issue with my java code. i asked the same question yesterday.
I asked a question about bigints yesterday which was kindly answered. However, I have
Yesterday I asked the question on how I should save my files. After some
So I asked this question yesterday, but the goal posts have changed and the
This problem is related to This question I asked yesterday. Now it seems that
yesterday I asked question which have been answered correctly. Now I want spend more

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.