24

I am working on an iPhone app, and in a particular view I need to load two different entities: One that will populate a UITableView, and another that will populate a UITextView.

Is it possible to fetch both properties using a single NSFetchedResultsController?

Or do I need to use two different NSFetchedResultsControllers?

Any ideas on how to best approach this problem?

Greg
  • 9,068
  • 6
  • 49
  • 91
futureshocked
  • 2,105
  • 4
  • 23
  • 32
  • Can you be a little more specific about how your model is set up? Is there a relationship between the two entities? If so, you may be able to just traverse the relationship to get the values you need without doing two fetches. – Alex Jun 30 '10 at 14:40
  • Hi Alex, the two entities are not related. It is setup up like this: * Entity "Item_comment" represents comments that I want to show in an UITableView. The user can tap on any number of those comments to store them. * Entity "Inspection_data" represents a record of the Item comments text (not the entity, but the text of that entity only). The text of that record I want to display in a UITextView. – futureshocked Jul 01 '10 at 00:00
  • 3
    The only way to get multiple entity types returned in a single fetch request is if they both inherit from a common parent entity and you set the fetch entity to the parent. – Dave DeLong Jul 01 '10 at 04:44

4 Answers4

14

Each fetch request has only one entity and each fetched results controller has only one fetch. Therefore, you need separate controllers for each entity.

If you think about it, how would you make a predicate to fetch two logically separate entities?

You probably don't need two fetches at all. In most cases, you can fetch the entities that populate the table and then use a relationship for the entity of the selected row to populate something like a text view.

TechZen
  • 64,370
  • 15
  • 118
  • 145
  • 2
    Thanks. I added a second fetch controller, made sure it doesn't have a delegate, and it worked exactly as intended. Since I don't need to take any action once the second fetch controller has returned its results, I don't need to listen for changes. – futureshocked Jul 01 '10 at 00:53
  • 5
    There would be no problem is you did need to listen for changes. You just do a compare for each delegate method. if (controller == self.entity1FetchedResultsController){...} else {...} – Louis Cremen Mar 13 '13 at 07:21
  • I have a situation where I have two entities that sub-class a super entity. In this case the fetch should be able to grab all of them in one controller. You then need to sort on some common attribute and check the class type to determine cell rendering. – Lee Probert May 03 '13 at 09:37
  • So sections count of NSFetchedResultsController is always 1 ? right ? because we can have only one Entity for one NSFetchedResultsController.. – Mani murugan Aug 12 '14 at 06:23
10

Best solution would be to refactor your Model and see if your 2 entities have something in common. You can make an abstract entity for the intersecting stuff, then inherit your 2 entities out of that. Perform the fetch on the abstract entity, and your fetch results controller should return mixed results.

Fervus
  • 710
  • 7
  • 18
7

As TechZen stated, the answer is no.

However, you can monitor the saves of the NSManagedObjectContext yourself and react to those saves. If you really do need to watch more than one entity (something that is far more common on the iPad than the iPhone) then add a NSNotification observer on the NSManagedObjectContextDidSaveNotification and look at the -userInfo of the NSNotification that comes back. You can then run predicates on against the results to determine if you need to update your display. That is what the NSFetchedResultsController is doing under the covers.

Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182
0
The quick answer is NO. But I found a creative answer.

In your tableViewController, make a search bar with how many scopes you have.

When different scope is selected, you can fetch different entities!

This works because I made an app like this!

Users would have easier time separating the two different data too!

coolcool1994
  • 3,704
  • 4
  • 39
  • 43