views:

162

answers:

1

I'm checking out the default Xcode template for an iPhone Core Data project. In the method that returns the fetched result controller I see this:

- (NSFetchedResultsController *)fetchedResultsController {

    ...

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
    ...

    return fetchedResultsController;
}    

It seems to be setting specific entity name. What If I have multiple entities? Would I have 2 NSFetchedResultsController instances and have 2 methods that return the correct controller depending on which entity I'm using?

Thanks

+2  A: 

It depends.

For example, if you have a Person entity and Employee entity that inherits from Person, then you can use one NSFetchedResultsController for a Person entity that would fetch both Persons and Employees. However, if you have something like Fruit entity and Person entity (and Person does not inherit from Fruit and vice versa) then it's unlikely that you can use 1 NSFetchedResultsController to get Fruits and Persons.

Whether or not you need 1 or more NSFetchedResultsController depends on your entity inheritance hierarchy.

Giao
In my app I have 2 entities. Task and Bin. A bin contains multiple Tasks, so the Bin entity has a To-Many relationship to the Task entity called "tasks". Each task has a "bin" relationship to the Bin entity. And I have an inverse relationship configured between the two. Bin does not inherit from Task and vice versa, but they do have a relationship. What would be the best option in my scenario?
macatomy
What does your table view display? Tasks or Bins? A common UI pattern would be to display Bins and then display Tasks in a particular Bin when the user selects a particular Bin (master-detail view).
Giao
Thats exactly what I'm doing. The master view has a table view that lists the bins, then when a bin is selected, the detail view has another table view that displays the tasks that are in that bin.
macatomy
The typical approach for this on the iPhone platform is to have a UINavigationController handling 2 UITableViewControllers: one for Bins and one for Tasks. When a Bin is selected the table view controller for Tasks loads all the Task entities relating to the selected Bin and displays it. I think you're trying to do both in one UITableViewController which is entirely possible but with a lot of complications.
Giao
I'm sorry if I wasn't clear earlier, I am using 2 UITableViewControllers (one for bins and one for tasks, as you mentioned). So going back to the original question, would I use 2 NSFetchedResultsControllers here? Thanks
macatomy
Yes, you'd use two different NSFetchedControllers: one for fetching Tasks in one UITableViewController and one for fetching Bins in the other UITableViewController.
Giao
Great, that answers it :-) Thank you.
macatomy