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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:19:44+00:00 2026-05-28T02:19:44+00:00

I have this Core Data Model Hierarchy, with the respective relationships to each other:

  • 0

I have this Core Data Model Hierarchy, with the respective relationships to each other:

COMPANY -> DIVISIONS -> DEPARTMENTS -> EMPLOYEES -> PHOTOS

If I want to collect all the employee photos from a division I need to:

  1. Fetch to find what departments have a division.
  2. Fetch to find all the employees in each department in the division
  3. Fetch each employee to find if there are photos….
  4. Add the photo to the array.

Which are 3 requests inside loops, performance wise is a disaster!

-(NSArray *)getPhotosForCategory: (NSManagedObject *)division {

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Divisions"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"division == %@", department];
    [request setPredicate:predicate];

    NSError *error;
    divisions = [managedObjectContext executeFetchRequest:request error:&error];

    NSMutableArray *employeesArray = [[NSMutableArray alloc] init];

    for (NSManagedObject *object in divisions) {

        NSFetchRequest *employeesRequest = [NSFetchRequest fetchRequestWithEntityName:@"Employees"];
        NSPredicate *employeesPredicate = [NSPredicate predicateWithFormat:@"department == %@", object];
        [employeesRequest employeesPredicate];

        NSArray *employees = [managedObjectContext executeFetchRequest:employeesRequest error:&error];

        [employeesArray addObjectsFromArray:places];

    }

    NSMutableArray *photosArray = [[NSMutableArray alloc] init];

    for (NSManagedObject *object in employeesArray) {

        NSFetchRequest *photosRequest = [NSFetchRequest fetchRequestWithEntityName:@"Photos"];
        NSPredicate *photosPredicate = [NSPredicate predicateWithFormat:@"employee == %@", object];
        [photosRequest setPredicate:photosPredicate];

        NSArray *photos = [managedObjectContext executeFetchRequest:photosRequest error:&error];

        [photosArray addObjectsFromArray:photos];

    }

    return photosArray;

}

anyone can suggest a better solution or how such requests can be done?

  • 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-05-28T02:19:45+00:00Added an answer on May 28, 2026 at 2:19 am

    Assuming you setup inverse relationships for everything:

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Photos"];    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"employee.department.division = %@", division"];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Core Data model similar to this: Company Entity companyName attribute (string)
I have a core data model object called Entry. In this I have an
Myself and a co-worker have this issue - We have a core data model.
I have a basic Core data model like this: Class -Class Name (string) Relationship:
Lets say I have a core data model like this one: Item attributes: name
I'm following this how-to to implement Core Data storage in my app: I have
My Core Data model looks like this: alt text http://fwdr.org/lg4q The players relationship of
I have a core data model with two tables (meal and ingredients). I am
This is the Core Data object model I am working with (I am a
I have the following core data model with two entities: entity item which holds

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.