One-to-many NSFetchedResultsController

This is something that I just can’t understand when working with Core Data. I want to work with NSFetchedResultsControllerDelegate, and the regular code that I have seen so far is easy to understand, but always based on one entity model. Thus, you want to show all the β€œevents” in your table, you make a query for sampling in the Event entity and there, you are ready to work.

The fact is that my model:

City (one-to-one) Company (one-to-many) Employees 

My table should show employees, but the choice should be based on the city to get the Company, and then the Employees, right? I completely lost this, I just don’t see how to make a selection.

Because if I get a city or company and I put employees in an NSMutableSet, do I not lose all the standalone UITableViewController synchronizations? For example, if I do it this way, I will not be able to do something like

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section]; return [sectionInfo numberOfObjects]; } 
+4
source share
3 answers

The data model must have a reciprocal relationship, so when you receive a particular object, you have immediate access to all related objects.

In your case, the reciprocal data model would look something like this:

 City{ company<-->Company.city } Company{ city<-->City.company employees<-->>Employee.company } Employee{ company<<-->Company.employees } 

So, if you have an Employee object, you will find a company with self.company and the city self.company.city (in most cases, the actual self not needed, and I use it to illustrate). If you have a Company , you will find employees in self.employees and a city in self.city . If you have a City facility, you will find all employees with self.company.employees .

Relationships are what actually create an object graph based on Core Data. You use selections to find one group of objects in a graph, and then you β€œmove” external relationships with these objects to find all related data. This is a mutual relationship that allows you to go back and forth on relationships in both directions.

+2
source

Better late than never.

Use predicate

 predicate = [NSPredicate predicateWithFormat:@"(ProvinceToCounty == %@)", selectedObject]; 

Follow this link, http://blog.sallarp.com/iphone-core-data-uitableview-drill-down/

If you plan to create a common class and do this work for different parent children, be sure to remove the cache name NSFetchedResultsController.

+4
source

I think you should get an object managed by "Company" with a filter for the City attribute or the Company Name attribute, or both. If you have established a one-to-many relationship between the company and the employees, then your company-managed object must have the NSSet property, which has all the employee objects that you need for your list.

0
source

All Articles