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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex