Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core data: can NSFetchedResultsController fetch two different entities?

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?

like image 323
futureshocked Avatar asked Jun 30 '10 07:06

futureshocked


People also ask

When should we use NSFetchedResultsController class?

NSFetchedResultsController is a very useful class provided by the CoreData framework. It solves many performance issues you frequently run into while reading a large amount of data from database and displaying that data using a UITableview, UICollectionView or MKMapView.

What is NSFetchedResultsController Swift?

A controller that you use to manage the results of a Core Data fetch request and to display data to the user.

What is Nsfetchrequest in Swift?

A description of search criteria used to retrieve data from a persistent store.

What is the use of nsfetchedresultscontroller in iOS?

In iOS, you use NSFetchedResultsController to connect the model (Core Data) to the views (storyboards). When you use Core Data with a UITableView-based layout, the NSFetchedResultsController for your data is typically initialized by the UITableViewController instance that will utilize that data.

What is a fetched results controller in Salesforce?

A controller that you use to manage the results of a Core Data fetch request and to display data to the user. While table views can be used in several ways, fetched results controllers primarily assist you with a primary list view. UITableView expects its data source to provide cells as an array of sections made up of rows.

Where does the initialization of the nsfetchedresultscontroller occur?

This initialization can take place in the viewDidLoad or viewWillAppear: methods, or at another logical point in the life cycle of the view controller. This example shows the initialization of the NSFetchedResultsController .

What are the four parameters of the fetch results controller?

When you initialize the fetch results controller, you provide four parameters: A fetch request. This must contain at least one sort descriptor to order the results. A managed object context. The controller uses this context to execute the fetch request. Optionally, a key path on result objects that returns the section name.


2 Answers

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.

like image 68
TechZen Avatar answered Oct 03 '22 04:10

TechZen


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.

like image 9
Fervus Avatar answered Oct 03 '22 04:10

Fervus