Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Core Data - Multiple NSFetchedResultsController in one UIViewController

I'm trying to build an iPad app working with Core Data. But I'm facing a design and a coding issue. Let's say I add one UIViewController to my window and inside that viewcontroller, I need to display two tableviews (2 different entities) and 2 views (2 others entities) (So I need to fetch 4 entites for one UIViewController). I only found tutorials explaining how to use NSFetchedResultsController with one UITableView !

My question is : Should I declare 2 UITableView and 2 NSFetchedResultsController in my viewController ? Or should I declare 2 UITableViewController and then inside each of them declare one NSFetchedResultsController ? What about the two others views ? I should use NSFetchRequest ? If yes, where ? inside the views or inside the UIViewController ?

I know it's a lot a questions :). Thanks in advance for your help.

like image 506
Dabrut Avatar asked Oct 20 '11 14:10

Dabrut


People also ask

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.

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 is the difference between fetched results controller and delegate?

The delegate notifies the table view controller when any changes have occurred to the underlying data structure. Typically, the table view controller is also the delegate to the fetched results controller so that it can receive callbacks whenever there are changes to the underlying data.

How do I use core data with the iOS user interface?

In macOS, Core Data is designed to work with the user interface through Cocoa bindings. However, Cocoa bindings are not a part of the user interface in iOS. In iOS, you use NSFetchedResultsController to connect the model (Core Data) to the views (storyboards).


2 Answers

Yes, you'll want to use 2 NSFetchedResultsController, one per UITableView. NSFetchedResultsController takes a NSFetchRequest, so you'll want to setup your NSFetchRequests based on the data that needs to be displayed in the UITableViews.

You should put all this controller logic inside your UIViewController subclass.

It is fine to have multiple UITableViews inside a single UIViewController, just keep in mind the delegate and data source methods. You'll have to test the UITableView parameter to see which table is asking for data/delegation handling.

like image 157
logancautrell Avatar answered Sep 22 '22 07:09

logancautrell


How are you displaying two views at one time? However you're doing it, I'd suggest the two-controller method, perhaps with a wrapping class that holds both and controls any interaction between them. That seems to me to fit the best with the MVC paradigm.

like image 37
Kevin Avatar answered Sep 21 '22 07:09

Kevin