Multiple NSFetchedResultControllers for different entities?
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 fetchedResultsControlle开发者_开发百科r;
}
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
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.
精彩评论