Table of Contents Previous Section

Application Initialization

The application init method is invoked when the application is launched---either from the command line or by autostarting---and never again. It's common to initialize application variables in an application init method. For example, the follow excerpt from the Application.wos script in the DodgeDemo example initializes the models, categories, and priceRange application variables.

id models, categories, priceRange;
- init {
     id modelSource, categorySource;
    [super init];
    [self logWithFormat:@"Welcome to DodgeDemo!!!"];

    // code not shown... 

    // Create Model data source and fetch the models
    modelSource = [[[EODatabaseDataSource alloc] initWithEditingContext:
    editingContext entityName:@"Model"] autorelease];
    if (!modelSource) {
        [self logWithFormat:@"Cannot create model data source"];
        [self terminate];
    }
    models = [modelSource fetchObjects];
    // Create Category data source and fetch the categories
    categorySource = [[[EODatabaseDataSource alloc] initWithEditingContext:
    editingContext entityName:@"Type"] autorelease];
    if (!categorySource) {
        [self logWithFormat:@"Cannot create category data source"];
        [self terminate];
    }
    categories = [categorySource fetchObjects];
     // Price range for price browsers
    priceRange = @(8000, 10000, 12000, 14000, 16000, 18000, 20000, 25000, 
    30000, 50000, 90000);
  return self;
}

When scripted applications are run, WebObjects automatically creates an instance of a special subclass of WOApplication and adds to it the code from the application script. When you send init to super in an application script, you invoke the init method of the superclass of the instance: WOApplication. You can also create your own subclass of WOApplication and override init to perform any necessary initialization. It is more common, however, to implement the init method in an application script.

Because all applications are not reparsed after the first time, changing a scripted application init method has no effect on a running WebObjects application . To have changes to a scripted application's init method take effect, you must restart the application.

In addition to using the application init method to initialize application variables, you can also use it to configure the application's behavior. For example, you can set the application time-out period and specify the page-cache size:

// Set the number of transactions pages are persistent
[self setPageCacheSize:10];
// Set application timeout
[self setTimeOut:43200]; 

Table of Contents Next Section