Reduce the size of a Flex 3 application avoiding unnecessary references with the getQualifiedClassName() method
There are many ways you can use to control and optimize the performance of your Flex applications acting both on the server as well as on the client side. The first optimization you can implement is by reducing the time tha application takes to start.
Externalizing assets and searching for linker dependencies are simple approaches that can save the end user a lot of time in loading te Flex application.
It's common for developers that at the end of a Flex application they are inadvertently linking in some classes that they never use.
I've found that another common error is to let the compiler to include classes that the application won't use for its entire life. That's a shame !
So an important step to check before compiling and deploying a Flex app is to check that you're not including unused classes.
There is a useful method that helps you for this scenario : the getQualifiedClassName().
This class comes from the flash.utils package and it returns a String that contains the fully qualified class name of an object.
You can use it in such kind of situations where you have to compare one or more classes without linking them into the SWF file.
This is a simple scenario where we want to remove all the Button classes inside our application without causing that class to be linked into the SWF if not used inside the file:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
initialize="removeSomeChild()">
<mx:Script>
<![CDATA[
private function removeSomeChild():void
{
myTxt.text += "\n The number of children at the beginning " + numChildren;
for (var i: Number = 0; i <= (this.numChildren-1); i ++)
{
var childClassName:String = getQualifiedClassName(getChildAt(i));
myTxt.text += "\n This is the full name of the class: " + childClassName;
switch (childClassName)
{
case "mx.controls::Button":
removeChild(getChildAt(i))
}
}
myTxt.text += "\n Children alla fine: " + numChildren;
}
]]>
</mx:Script>
<mx:TextArea id="myTxt" text="Ciao" />
<mx:Button />
<mx:ComboBox />
<mx:TextInput />
</mx:Application>
The use of the getQualifiedClassName() method the following classic syntax ensures us to exclude the Button class from being compiled into the SWF file :
getChildAt(i) is mx.controls.Button
Note: Removing a DisplayObject from the DisplayList don't destroy the object neither remove it from Flash Player' s memory.The object will continue to exist even if not as a child of any other container.
Marco Casario is one of the most dynamic developers in the Adobe (formerly Macromedia) world. Flex and FlashLite certified Instructor, Flash and Dreamweaver certified, he intensively collaborates with Adobe Italy as a speaker and promoter of several events and roadshows. Marco has recently founded Comtaste S.r.l., a company dedicated to explore new frontiers in the Web 2.0 field, where the themes of accessibility and usability have added further importance to the PDF format and the relevant Acrobat application tools.
