Table of Contents Previous Section
Another way to control the amount of component state that's maintained between transactions is to make use of WOComponent's awake and sleep methods. Unlike the component'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 for DodgeLite's Main component that we looked at earlier could be changed to:
id models, model, selectedModels; id prices, price, selectedPrices; id types, type, selectedTypes; - awake { models = [[WOApp modelsDict] allValues]; types = [[WOApp typesDict] allValues]; prices = [WOApp prices]; } - sleep { models = nil; types = nil; prices = nil; }
Note that in WebScript you set a variable to nil to mark it for release; whereas, in Objective-C you send the object a release message:
- sleep { [models release]; [types release]; [prices release]; }
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.