views:

420

answers:

2

I have a UITableView which displays two different entity types -- each row could be either EntityA or EntityB. cellForRowAtIndexPath looks at the class type and determines how to create the cell.

I am persisting my objects using Core Data and would like to use a fetchedResultsController to manage this tableview because of the memory-management benefits. However, I cannot figure out a way to create a fetchedResultsController that can fetch more than one type of entity. Because the fetchedResultsController initializer takes an NSFetchRequest, and the latter only allows a single entity to be identified, I don't think what I want to do is possible.

Assuming it is not, what alternatives do I have? Could I do something like single table inheritance, creating an entity model that merges the features of my two entities (they are similar)? This would add complexity to my code and weaken the typing significantly but the user would never know the difference.

All suggestions welcome.

Thanks!

/afb

+2  A: 

There is no real way to overcome the 1 entity per NSFetchResultsController using this class.

However, if you still want to enjoy the benefits of using this class that takes care of handling big sets a of data, you can create two or more NSFetchedResultsControllers and use the methods of the UITableViewDataSourceProtocol to switch among them. This is especially easy if you plan to use a section for each of the entities.

Jorge Ortiz
A: 

You can do this easily in one fetch request. Simply use entity inheritance. Have EntityA and EntityB both be subclasses of some abstract entity, say EntityX. Then set the NSEntityDescription for your fetch request to be for EntityX. Not sure how to specify attribute keys for certain sub-entitis in your predicate though.

William Tian