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.
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.
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 .
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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With