Skip to main content

Which member do I put my code in?



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?


Comments

  1. The two classic places to put code are in the init and the loadView methods of the view controller. Ie

    - (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.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.