Table of Contents Previous Section

Session Initialization

The session's init method is invoked when the application creates the WOSession object for the current session, which happens when the application receives the first request of a new user. Initialize variables in the session init method that should retain their values between transactions throughout the session. For example, the Session.wos script in the Visitors example initializes the session variable timeSinceSessionBegain before setting up a timer that will result in the variable's value being incremented:

id timeSinceSessionBegan;
id timer;
- init
{
    [super init];
    timeSinceSessionBegan = 0;
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self
    selector:"timeOfSession" userInfo:nil repeats:YES];
    [self setTimeOut:120];
    return self;
}

Note: An important side effect of using a timer object in a WebObject's application is that the method invoked when the timer fires is outside the request-response loop. In other words, invocation occurs after the transaction concludes, and thus the method has no access to the WORequest, WOResponse, and WOContext of the transaction.

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

The WOSession object's awake method is invoked just after the object is created (and receives init) and immediately after being restored for each subsequent transaction.

Table of Contents Next Section