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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:02:05+00:00 2026-05-27T09:02:05+00:00

I’m sadly stuck with Core data. I’ve built a 1:M relationship, let’s say Company

  • 0

I’m sadly stuck with Core data. I’ve built a 1:M relationship, let’s say Company and Employees.

I create the Company, then I create the Employee:

Employees *employee = (Employees *)[NSEntityDescription insertNewObjectForEntityForName:@"Employees" inManagedObjectContext:myObjectContext];

employee.name = employeeName;
employee.company = company;

NSError *error = nil;    
if (![myObjectContext save:&error])
{
    // Handle the error.
    NSLog(@"error: %@", error.description);
}

If I analyze the sql file, I can see that the Parent column for the Employees record is correctly populated with the Company identifier.
Now I would like to select a Company in my tableview and show its Employees in another tableview. I implement the NSFetchedResultsController as follows:

- (NSFetchedResultsController *)fetchedResultsController
{
if (fetchedResultsController != nil)
{
    return fetchedResultsController;
}

//retrieve context
if (myObjectContext == nil)
{
    id appDelegate = (id)[[UIApplication sharedApplication] delegate]; 
    self.myObjectContext = [appDelegate managedObjectContext];
}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription                                   entityForName:@"Employees" inManagedObjectContext:myObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];

NSFetchedResultsController *myFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:myObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
self.fetchedResultsController = myFetchedResultsController;
fetchedResultsController.delegate = self;

return fetchedResultsController;
}

and I call it in viewDidAppear:

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

NSError *error = nil;
//call the fetch
if (![[self fetchedResultsController] performFetch:&error])
{
    NSLog(@"Error with fetchedResultsController: %@, %@", error, [error userInfo]);
    exit(-1);
}
[self.tableView reloadData];

}

The problem is that all the Employees, and not only those in the selected Company, are displayed.

Any ideas?

  • 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-27T09:02:06+00:00Added an answer on May 27, 2026 at 9:02 am

    Your fetch request is setup to fetch all employees with no condition so just as you are seeing, you are going to get all employees in your database. You either need to set a predicate on your fetch request using NSPredicate and have it only select Employees whose Company is the company you selected in the previous view. Or, the approach I would take, you have the Company object selected, pass its managedObjectId or the managedObject itself to your Employee tableview controller and use the relationship you setup to fetch the employees from it, Core Data will fill in any faults and fetch the related employee objects from the database for you.

    If you setup your relationship properly calling selectedCompany.employees will return an NSSet which you can use as your UITableViewController‘s data source.

    Edit

    If there are potentially a large number of employees in the relationship you may want to stick with the fetched results controller approach and setup the predicate appropriately in order to obtain the memory efficiency benefits that NSFetchedResultsController brings to the table.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm trying to create an if statement in PHP that prevents a single post
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.