November 27, 2010

Cocoa App Life Cicle

Every process has a beginning and an end, that's how we are able to describe the procedural steps involved to reach an objective. And of course, computational apps, are related to a certain steps necessary to execute a block of code. Most common example is the operative system, this is once we push the button every circuit runs a piece of compiled source so that after 10 - 40 seconds we are using it.

After this boring introduction, let's go to the matter of this post: How Cocoa Apps runs in MacOS X:


Most of the basic code is loaded into ram when the Application starts. Afterwards, the Main Window is loaded, commonly packaged into ".nib" crated in Interface Builder. For each object defined into it (NSTextfield, NSLabel, NSTableView) a message to "alloc" is sent, as the instance variables are set as well. Not all ".nib" files are allocated, just those that programmatically are called to initialize, this is with the message to that object of the awakeFromNib.

Arriving to this point, makes the Main Loop start a defined cycle where an event is read from queues, for example with the window functionally working, a button is pressed, so the action is read, put in a list of queues. When the time to respond to that request arrives, the event code is triggered and all the necessary pieces of code are loaded into memory, of course the views are redrawn with a "lazy" object allocation in mind.

Finally, the user closes every interface, the OS sends a message to the program delegate telling it that pieces of reserved memory will be freed, thus last blocks of code will be executed.

No comments:

Post a Comment