Archive

Posts Tagged ‘Flex’

When garbage collection does more than you want

September 4th, 2009 4 comments

I had an interesting Flex bug to solve a day before the project deadline. I was showing a progress bar while some images were preloading in the background. The application was only allowed to fire applicationComplete event once these images have finished loading.

The client complained that the progress bar stalls the first time you run the app in the browser. Refreshing the page causes the application to start immediately, as if everything has been loaded. I reproduced the bug with bandwidth throttling. That made me think: “the assets are being fully loaded, but the progress bar doesn’t seem to care”.

With further investigation, I realized that I solved the same problem over a year ago. When one creates Loaders on the fly, such as in a loop, one often does not keep any reference to these Loaders outside of the scope of the function. Since it is a good habit to make event listeners weak, it is possible for the garbage collector to flag these Loaders for removal before they even have a chance to fire their complete event.

So to make sure that the progress bar does not stall, I needed to keep a reference to the Loader that I create within my loop. I simply pushed every Loader to an array. Once everything finished loading and the application started, I cleared the array to allow the garbage collector to take care of the rest.

Problem

Progress bar stalls while preloading images.

Cause

Loaders with weak event listeners and no references to them within the application.

Solution

Use an array to store references to the Loaders until all of them have finished loading.

LinkBar was meant to be used with a ViewStack

July 27th, 2009 No comments

I had to read the source code for LinkBar and its parent NavBar to understand why my LinkBar wasn’t highlighting the clicked item. Turns out, it’s because I use an ArrayCollection as a dataProvider. According to the documentation and the source code, it is possible to use an IList (such as an ArrayCollection), a String (that refers to a ViewStack in the current document) or an actual ViewStack object. But, when a selection occurs, LinkBar makes sure that we supplied a ViewStack before allowing the highlight.

Why, I have no idea. All I know is that I want to use an ArrayCollection as my dataProvider and still highlight clicked elements. I extended the LinkBar component with a tiny workaround. Feel free to use it as you wish.

StacklessLinkBar – forgive me if you find the name unoriginal.

Flex: non-UIComponents in the Display List

July 17th, 2009 No comments

There are 2 main ways to add non-UIComponents to a Flex Container.

1. You may add the object to a SWFLoader, which does not require its children to extend UIComponent.

2. You may add the object to Container.rawChildren.

You have to be careful with the 2nd one. First of all, know that whatever is contained in Container.rawChildren is not sized, positioned or rendered automatically. You cannot, for example, put a button in there. Another thing is that anything added to Container.rawChildren is NOT contained in the Container.children, but the contrary is true. Relying on indices to remove from the display list might be tricky.Here’s what I mean:

  • You add 5 objects using Container.addChild()
  • You add 5 objects using Container.rawChildren.addChild()
  • You want to remove the last child of Container.rawChildren, and will use index 9 instead of 4, because rawChildren returns all 10 children.

The reason for Container.rawChildren is to add skins, not actual content. Container.getChildren() returns only elements that were added using Container.addChild(), not elements that were added using Container.rawChildren.addChild().

So my suggestion to you would be to use rawChildren only if you do not intend to remove these children later, like skins. If you wish to add interactive elements and other content, create a SWFLoader and add them to it. And if you have a UIComponent, always add it to the Container using Container.addChild().

Flex 3D and Wiimote

March 29th, 2009 1 comment

I haven’t blogged in a while. Too much work to catch after the conference.

I’m having a lot of fun with 3D in Flex lately. I found this really great open source library papervision3d. It’s incredibly easy to use. I got the hello world to run in my existing Flex app in under 5 minutes. It’s not using the native Flash 3D so it can be run in Flash Player 9.

After reading their blog, I also found out about the SourceBinding framework (currently in alpha stage). However, the following video is a must-see for all gadget lovers.