Table of Contents Previous Section
Using awake and sleep
Another way to control the amount of component state that's maintained between cycles is to make use of WOComponent's awake and sleep methods. Unlike WOComponent's init method that's invoked just once in the life of the component, a component's awake and sleep methods are invoked at the beginning and end of any request-response loop that involves the component.By moving a component's variable initialization routines from its init method to its awake method and implementing a sleep method to release those variables, you can reduce the space requirements for storing a component. For example, the code shown in the section "Component Objects and Component State" could be changed to:
// rewritten Main.wosNote that in WebScript you set a variable to nil to mark it for release. In Objective-C you send the object a release message:
id models, model, selectedModels;
id prices, price, selectedPrices;
id types, type, selectedTypes;
- awake {
anApplication = [WOApplication application];
models = [[anApplication modelsDict] allValues];
types = [[anApplication typesDict] allValues];
prices = [anApplication prices];
}
- sleep {
models = nil;
types = nil;
prices = nil;
}
- sleep {Of course, what you save in storage by moving variable initialization to the awake method is lost in performance, since these variables will be reinitialized on each cycle of the request-response loop.
[models release];
models = nil;
[types release];
types = nil;
[prices release];
prices = nil;
}
Table of Contents Next Section