I'm writing my first SIMPLE DB app, basically, an offline db, select a listing of records, and then, click on one of those to display on the detail.
I looked at an older book for an example (pre xcode 4) because it had an example very similar to what I needed to do. So the example set up all the methods I'd need to access the database in a member called DBAccess.m AND I can tell from the debugger that the code visits main.m and then MasterViewController.m it executes awakeFromNib and didViewLoad which is awesome... BUT where do I put my first statement that calls the routine I need that's in my DBAccess.m file? The book assumes you KNOW where to put your code and leaves it as an exercise for the user... ugh.
I can't find any definitive statement about how you insert your code into the execution cycle.
Is there a default execution cycle?
The two classic places to put code are in the init and the loadView methods of the view controller. Ie
ReplyDelete- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Put DB code here
}
return self;
}
Or
- (void)loadView
{
[super loadView];
// Put DB code here
}
The choice will depend on what you are doing with the data.
If the data is going to be displayed in UI elements from a NIB file, then you have to be a little careful as those elements are not likely to exist yet when the init method runs. Thus assigning DB data to UI elements in init can result in a program that looks right and compiles, but does not display any thing on the display (Been there, done that!) Thus you are better off delaying the DB data until the loadView fires.
If the data is not needed onscreen immediately (or is required for other non-display reasons) then you can load it in the init method. But the caveat there is that you are now consuming memory for things that may never be displayed - thus defeating any lazy loading during the loadView method.
But it all comes down person preference.