Learning Flex 4 – States

States are easier to work with in Flex 4 than they were with 3. While Flex 3 had the advantage that everything to do with your states was in one place, it made things more complicated by the additional syntax you had to deal with to add/remove items etc. With Flex 4, you can just add your state name to a property to specify a value specific to that state and specify if components should be included in/excluded from a state. The only downside to this is that your state specific code gets scattered but Flash Builder has an option to show you components by state so that helps there.

Starting from the beginning, you first need to declare what states you are going to have. This is done using the <s:states> tag. Within this, specify one or more <s:State> tags with the name property set to the name for your state. The first State is used as the default. In addition to the name, you may also specify a stateGroups property. This allows you to group states so you can refer to the group rather than all the individual states. This could be useful in the case where for most of the time, you want to do the same thing for several states but for specific components/properties you need a finer distinction.

Here’s an example (I like to use all caps for my state names to help them stand out a little more):

<s:States>
<s:State name="LOADING"/>
<s:State name="EMPLOYEE_VIEW" stateGroups="VIEW" />
<s:State name="MANAGER_VIEW" stateGroups="VIEW"/>
<s:State name="MANAGER_SELECT" />
</s:States>

The grouping is a bit artificial in this case but this app starts in the LOADING state while it’s gathering data from the back end. Then, if you’re a manager, the state becomes MANAGER_SELECT where you can select an employee to view data on, finally you get a MANAGER_VIEW of the data or the EMPLOYEE_VIEW if you logged in as an employee. You can see how there might be several components common to the MANAGER_VIEW and the EMPLOYEE_VIEW which you could use the VIEW stateGroup for but some components (maybe a change pay rate button for instance) would be specific to the MANAGER_VIEW.

You can include components in a state by using the property includeIn or exclude them using excludeFrom.  You can specify more than one state by using commas to separate them in the string (or specify a stateGroup). By adding the state to a group container, you can affect all the components within that group.

You can change properties (including event handlers) by suffixing the property name with the state name you want it to apply to eg – label.LOADING="retrieving data..."

If you don’t specify a state, the component/property is considered to be the same for all states. You can change the state by setting the currentState property (by setting it to the empty string, you return to the default or first state).

Custom components can declare their own states and you can use the currentState property of the instance of the custom component to change the state for that particular object.

When the state changes, the enterState event is fired by the State object being entered and by components entering that state. The exitState event is sent by the State and components being left. The object the currentState is being changed on fires currentStateChanging when it’s about to change and currentStateChange when it’s done.

You can use states within a custom item renderer to react to view states such as hovered, selected and change the presentation for these cases. This article has the details. Saturn Boy has also written a blog post looking at states with skinning.

Learning Flex – Lesson 26, The Basics of Memory Management and Performance

There are two basic ways to handle memory allocation. The first (used by C,C++) is to make the developer deal with it – you are responsible for allocating memory required and freeing it when you’re done. This allows the developer the freedom to decide when time consuming memory operations are done but the responsibility to keep track of everything or run into memory leaks (when unused memory isn’t released) or null pointer issues and the like when it’s released too soon.

The alternative chosen by Flash (and Java) is to use the runtime for garbage collection – i.e. let it decide how to allocate and free memory. The downside to this is that garbage collection can be time consuming and it’s nondeterministic – the runtime decides when it’s going to happen, not the developer.

There are several different algorithms for  garbage collection. The Flash player uses a version comprised of “reference counting” and “mark-and-sweep”. (For a discussion of various garbage collection strategies see this article).

For reference counting, the runtime keeps track of all the active references to an object. Each object keeps a reference internally to its children and each child has a reference to its parent. There are also references created explicitly by the developer as a way to access the object in question. If the reference count is zero, nothing can access that object any more so it can be removed and it’s memory reclaimed.

The second phase, mark-and-sweep is more costly and required to discover circular references – i.e. objects that reference each other but have no outside references. To perform the mark, the player starts at the root of the application and traverses through its references marking each as it goes. When it’s reached the leaf nodes of all those references, any objects not marked can be “swept” away and deallocated. Due to its cost, this operation is performed iteratively over several frames and is only run occasionally.

What does all this mean to a Flash/Flex developer? Well you should be sure to remove all references to an object when you no longer need it. Often this will be done for you with variables going out of scope etc but you should be proactive in removing longer lived references you no longer require.

Event Listener Leaks

Objects that want to be informed of an event can register as a listener using the addEventListener() method. This results in the creation of a reference to the object doing the listening. Therefore if an object is listening for events, it may not be available for garbage collection.

There is a method called removeEventListener() that can be called to remove the reference when it’s no longer required which would allow the listener object to be removed when it has no other references. Unfortunately, it’s not always easy to know when to call this.

An alternative when calling addEventListener() optional parameters may be added, the last of which is a Boolean indicating a weak reference (the default is false). What this means is that if set true, the reference created for the listener is considered weak and not counted as a reference for garbage collection. A listener with only weak references remaining would be eligible for garbage collection. Here’s a post with more on weak references and here’s an opinion on when they shouldn’t be used.

Flash/Flex Performance tuning

FlexBuilder (and now FlashBuilder) professional comes with a profiler tool that allows you to investigate the memory usage of your application and time operations to look for areas in need of tuning.

There are no free alternatives that I’m aware of but you can use flash.utils.getTimer() to time operations and System.totalMemory to keep track of memory use in your application. Grant Skinner also has a performance test harness you might find useful.

A great resource with lots of links to more detailed information on memory management and performance is this page on Jun Heider’s blog.

Learning Flex – Lesson 25, Debugging, Logging and Error Handling

Debugging

Perhaps the simplest form of debugging is adding a trace() statement to your code to print out when a function is entered or the value of a variable at a particular point.

In addition to the global trace() method, the <mx:TraceTarget/> tag allows you to see communication with a remote server. This can be particularly useful in determining what (if anything) your server is returning for your requests.

TraceTarget actually sets up the Flex logging API. The appearance of the server communication is down to the fact that the internal Flex components mx.rpc.*, mx.messaging.* and mx.data.* all use this API. We’ll come back to logging later.

A more interactive strategy for finding errors is to use a debugger. If you’re fortunate enough to have access to Flex Builder, you have a built-in graphical debugger that will allow you to set breakpoints, inspect variables etc.  If not, the Flex SDK comes with a command line debugger – fdb. This can be found in the bin directory and started by typing fdb and then run location of swf or just fdb location of swf directly. You can use help or tutorial at the fdb prompt to get information on the commands available. Basically you can set breakpoints (even conditional ones which the Flex Builder version doesn’t support), step through code and print or change the values of variables. It’s not quite as easy to use as the Flex Builder version but it will get the job done. There are also some 3rd party free tools available such as Demonster Debugger which can give you some of the features of a graphical debugger along with memory usage and frame rates but unfortunately, don’t support breakpoints.

Here’s Adobe’s info on the command line debugger and here’s a great chapter from the book Programming Flex 3 that OReilly have kindly put on their website that covers the command line debugger, the Flex Builder debugger and the logging framework.

Logging

The aforementioned OReilly chapter covers this topic really well. You can define different levels of logging (debug, warn etc) and filter on which you want to be logged. You can also determine what happens to your log data so for instance, you could arrange to log particularly worrying errors back to your server or perhaps to a local file that support staff could request users email them if problems occur.

Error Handling

Like most modern languages, ActionScript3 has the concept of exceptions and the associated try, catch, throw and finally commands.

Runtime exceptions occur during the operation of the application, often due to unexpected input or problems communicating with remote systems. In these situations, you can place the potentially troublesome code within a try-catch block like so:

try{
some dangerous code
}catch(e:Error){
do something about the error
}

Within the catch block, you can access properties and methods of the Error object: message; name; getStackTrace() – for debugger versions of the Flash player, returns the call stack for the error as a String and toString() which returns “Error” by default or the value contained in message if defined.

There are a number of error classes defined both by ECMAScript and ActionScript, more details on which can be found here.

You may define multiple catch blocks to deal with specific error classes. The rules are that the first catch block to satisfy the error will be executed and only one catch block may be run. For this reason, you should never use the base class Error before other catch blocks as it will always take the error.

The throw statement allows you to raise an exception which will bypass the normal operation of your application. You would normally do this if you have discovered a condition that prevents the normal flow of execution. You may either throw an existing error type or create your own by extending the Error class.

The finally statement should be placed after all catch blocks and is used to define code that should be executed regardless of whether an exception was raised or not (generally this is used for something like freeing resources requested in the try block).

Flash player 10.1 introduces the concept of a global error handler so you can trap errors that have otherwise not been processed. This allows you to have a catch-all point where you can at least log something and potentially inform the user before gracefully exiting.

According to the release notes (pdf), the current beta 2 of 10.1 has this functionality disabled but here’s a blog post from Christian Cantrell on how to do it using Air and here’s some more detail about the UncaughtErrorEvent.

Learning Flex – Lesson 24, Using Shared Objects

It’s often useful to be able to store data on the client side such as preferences or a saved cart similar to the way websites do using cookies.

Flex has the SharedObject class to achieve this which improves on cookies by being able to store complex data structures.

Properties assigned to a SharedObject are written to a file as soon as the swf is removed from Flash Player either by changing sites or closing down the browser. It’s also possible to manually force the write in the application.

Shared objects are stored on the end users machine in a location determined by the operating system (you can find out the details here ). They have an .sol extension and by default can each be up to 100kb in size (there’s no limit on their number). The user can use the flash player preferences to change this (right click in Flash window, select settings).

Shared objects cannot contain methods and are not deleted by clearing cookies. Like cookies, shared objects cannot be read from different domains. Note that when testing with Flex Builder, shared objects are limited to the same application as the testing environment opens a local file and does not establish a domain.

The static getLocal() method of the SharedObject class retrieves an existing SharedObject or creates a new one. If you need to write the object to file, you can use the static flush() method. Here’s an example of creating a SharedObject:

var mySO:SharedObject = SharedObject.getLocal("shoppingCart");

This creates a file called shoppingCart.sol on the users machine and the SharedObject mySO to refer to it.

To populate the object with data, you assign your values as properties of the data property of the object. For example, to store an email of myEmail@mail.com you would use:

mySO.data.email="myEmail@mail.com";

To store complex data objects, you must first create them so to store an array of products you would use:

mySO.data.products=new Array();
mySO.data.products=productsToSave;

When using the flush() method, you may specify a value for the size of the file in bytes. This can be used to create a file larger than initially required. The reason for this is that for the initial write of the file and any time it needs to grow in size, the user will be prompted for approval. by requesting a file size as large as you expect to get, you prevent extra requests for approval every time the shared object is written to.

Flash Player raises an exception when a flush() fails. It returns SharedObjectFlushStatus.FLUSHED
when it’s successful and SharedObjectFlushStatus.PENDING when the size of the file must increase and the user is being prompted for approval, after which a  netStatus event is dispatched with an information object detailing the success or failure of the request.

When working with a shared object, it’s best to check that the value you are about to use is present before attempting to use it. You can achieve this  as follows:

if (mySO.data.email != undefined){
//use the email address
}

To empty and remove the shared object, use it’s clear() method.

When using SharedObjects in a swf, make sure your display object is at least 215 pixels wide and at least 138 pixels high or the dialog prompt to increase storage cannot be displayed and the flush() call will fail.

Learning Flex – Lesson 23, Printing From Flex

Flex allows for printing from an application by using the FlexPrintJob class which is a wrapper for the flash.printing.PrintJob class. This allows you to specify one or more objects to be printed. The objects can be containers or custom components and you can specify a separate format that is not visible to the user.

The basic process is as follows:

  1. Instantiate a FlexPrintJob object
  2. call it’s start() method to begin the print process – this will cause the OS to display a print dialog and returns  true if they accept or false if they cancel out.
  3. Add the object(s) to be printed using the addObject() method
  4. Send the job to the printer using the send() method
  5. clean up any temporary objects no longer needed.

Between the start() and send() calls, a print job is spooled to the host operating system so only print specific work should be performed at this time.

It’s good practice to check the return of the start() method and abort the print attempt if it returns false.

If you try to hide the print version of a component by setting it’s visible property false, it will still occupy space in the layout. An alternative is to define the print layout in a custom component then define and instance and add it in your print process using the addchild() method of the application. You can then remove it using removeChild() as part of the print job cleanup. Your print code would then look something like this:

var printJob:FlexPrintJob = new FlexPrintJob();
if(printJob.start() != true){
  return;
}
var myPrintcontainer:CustomPrint = new CustomPrint();
addChild(myPrintContainer);
printJob.addObject(myPrintContainer);
printJob.send();
removeChild(myPrintContainer);

The output can be scaled by adding a parameter to the addObject() call. The options are static constants from the FlexPrintJobScaleType class:

  • MATCH_WIDTH – scales the printed output to match the page width. If the height exceeds the width, the output will span multiple pages (default)
  • MATCH_HEIGHT – scales the printed output to fill the page height. If the width exceeds the height, the output will span multiple pages.
  • SHOW_ALL – scales the printed output to fit on a single page, filling the smaller of width or height.
  • FILL_PAGE – scales the printed output to fill at least one page. It selects the larger of width or height.
  • NONE – the printed output matches the dimensions of the object on the screen.

To aid with printing data grids, there are print versions of data grid classes namely PrintDataGrid, PrintAdvancedDataGrid and PrintOLAPDataGrid. These have default appearances designed for printing and methods to support multiple page printing.

A PrintDataGrid will only print the rows that are visible by default. The property sizeToPage specifies that partially visible rows are not printed if it’s set true (default). To print rows below a scrollbar, you must use the nextPage() method and validNextPage boolean property.

While validNextPage is true, call nextPage() and then add the PrintDataGrid object to the print job.

Flex printing can look somewhat blurry on the page and does not always produce the results desired. For a great series of posts on an alternative, (AlivePDF) take a look at these posts from Kalen Gibbons.

Learning Flex – Lesson 22, Creating Transitions and Behaviors

Behaviors and transitions allow you to add animation and sound effects to your application. Behaviors are effects that can be applied directly on components, transitions are these effects applied to application states.

Some of the effects available are: fade, dissolve, move/resize, rotate, wipe, glow and play sound.

These effects can be used to draw the user’s attention to items changing on the screen or give cues as to which elements they can interact with.

Behaviors

When applied to components, behaviors are made up of a trigger and an effect.

The trigger is the action taking place on the component such as a button click or gaining focus. It is important to note that triggers are different to events. A component would have a mouseDownEffect trigger as well as a mouseDown event. When you use a trigger, you do not specify an event handler, instead you specify the effect to occur.

You can directly specify an effect class name for a trigger such as:

<mx:VBox showEffect="Fade"/>

but in this case, you are limited to one effect and cannot customize it.

You may define an effect tag and specify desired properties that can then be provided to a trigger using data binding on the effect id such as:

<mx:Fade id="myFade" duration="2000"/>
<mx:VBox showEffect="{myFade}"/>

You can apply multiple effects to the same trigger by using the <mx:Sequence> or <mx:Parallel> tags by nesting effects within them to occur in order (using <mx:Sequence>) or all together (using <mx:Parallel>). You can even nest these tags for more involved effects.

As effect triggers are implemented as CSS styles, you can specify them using an <mx:Style> tag:

<mx:Style>
List {showEffect : myFade;}
</mx:Style>

which will make all lists have the same show effect (unless you override it in a specific list configuration, or:

<mx:Style>
.fadeStyle { showEffect : myFade;}
</mx:Style>

which will display this effect for all components with the styleName property set to fadeStyle.

It’s also possible to play effects at other times by calling the effect’s play() method (you can optionally specify an array of components to perform the effect on and a boolean value which when set to true, will play the effect backwards. There’s also an end() method to end the effect (this is often called immediately before play() to stop any outstanding effects).

Effects can be used for changes in data provided by a dataProvider in a List or TileList. More information about this can be found here.

More control over the animation of an effect may be provided by using an easing function. All effect classes that are children of the TweenEffect or MaskEffect classes support the easingFunction property. Some components have easing function style properties that can be used directly.  More information on easing functions can be found here.

You can play a sound effect by using the <mx:SoundEffect> tag and specifying the url of an MP3 file in the source property. Alternatively, if you’ve already embedded  the sound file, you may use data binding to refer to it’s class object here.  There are also several properties to determine the duration, start point in the file, volume start and end values etc (you can find out the full details here) .

Transitions

Transitions are effects for application states. They must be declared as children of an <mx:transitions> tag (this is a property of the application). Each <mx:Transition> has a fromState – the view state you’re moving from, toState – the view state you’re moving to and the effect object you want to play when the transition occurs. You can specify a target or targets (array of target objects) for the effect either in the effect tag directly or at a higher level within an <mx:Sequence> or <mx:Parallel> tag. An example would be:

<mx:transitions>
<mx:Transition fromState="START" toState="*">
<mx:Dissolve duration="2000"  target="{myContainer}"/>
</mx:Transition>
</mx:transitions>

Once the transitions are declared, they are triggered by changing the application’s currentState property.

Learning Flex – Lesson 21, Deploying Flex Applications

Adobe Integrated Runtime (AIR) allows you to write Flex applications that can be run from the desktop. AIR gives you access to features that are not available when running from the browser such as interaction with the local file system, an embedded database for client side storage and with the new AIR 2 beta , raw microphone data access and multitouch support amongst other things.

The basics of working with AIR are very similar to Flex applications. In Flex Builder or FlashDevelop, choose a new AIR project and instead of a root <mx:Application> tag, use <mx:WindowedApplication>.

In defining your application, the application id is important as this is used to register the app with the operating system so it needs to be unique. This value (along with other configuration data) is stored in the project’s application.xml file. Flex Builder allows you to specify the application id on project creation but FlashDevelop will default it to the project name so you’ll probably want to go in and change that. Further information on the application.xml file can be found here.

Because of the additional access they have, AIR apps need to be signed by a security certificate before they can be released.

You can create a self-signed cert or buy one from a certificate authority such as VeriSign (currently $499 per year) or Thawte (currently $299 per year). Note these are different to standard SSL certificates.

The downside to a self-signed cert is that the install screen will show a big red “?” along with the text “are you sure you want to install this application to your computer?” instead of the happy green “!” that you get with a valid paid certificate. You can find out more about how to get and install a certificate in this article.

FlashDevelop provides two batch files and an AIR_readme.txt file when it creates an AIR project to help you release your application. You can find out more information on packaging an AIR file (including potential error messages) in this article.

Follow

Get every new post delivered to your Inbox.