I have a UIViewController subclass which is having its view created programmatically within the loadView method. The primary purpose of the view is to display information that is fetched from a store via core data. How the view is created will be different depending on how many entities are received from the fetch, so I need this information ahead of time. I believe all of the tutorials and sample code that I have seen so far shows fetch requests being executed in viewDidLoad (or later), so I just wanted to ask if there are any reasons to avoid doing this in loadView. Does it make any difference if I use fetches in loadView like so or in viewDidLoad? Thanks.
- (void)loadView {
[super loadView];
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
///
}
//...go on to load the view
}
If you are loading data once per setup-tear down cycle of the View Controller then putting it in loadView should be fine.
However if you have any buttons or UI inputs that may cause it to be reloaded, you may want to create your own reloadData method in your View Controller.
Take a look at some examples of UITableView with Core Data to get a feel for a common approach to loading core data objects in Cocoa. The UITableView uses the fly wheel pattern, meaning it reuses the table cell (row) objects and just inserts new data as it goes.
If you have lots of entities returned by CD, consider using a UITableView and you can style it up to look quite different from the norm if you need to.