Go Premium for a chance to win a PS4. Enter to Win


ResultEvents not firing on multiple Flex3 WebService calls

Posted on 2009-04-08
Medium Priority
Last Modified: 2013-11-11
I have a user form in Flex that has three drop-downs that need to be initialized. I have created a class that wraps the web service methods:

            public function GetShipments( custId:int ):void {
                  ws.addEventListener( ResultEvent.RESULT, WebService_Results );
                  var callToken:AsyncToken = ws.getShipmentsForCustomer( custId );
                  callToken.marker = "shipments";
            public function GetRoutes( custId:int ):void {
                  ws.addEventListener( ResultEvent.RESULT, WebService_Results );
                  var callToken:AsyncToken = ws.getRoutesForCustomer( custId );
                  callToken.marker = "routes";
            public function GetDevices( custId:int ):void {
                  ws.addEventListener( ResultEvent.RESULT, WebService_Results );
                  var callToken:AsyncToken = ws.getDevicesForCustomer( custId );
                  callToken.marker = "devices";
            private function WebService_Results( e:ResultEvent ):void {
                  var results:ArrayCollection;

                  switch ( e.token.marker ) {
                        case "shipments":
                              trace( "got shipment results" );
                              results = Route.ArrayToTypedArray( e.result as ArrayCollection );
                              dispatchEvent( new ShipmentsEvent( ShipmentsEvent.LOAD, results ) );
                        case "routes":
                              trace( "got route results" );
                              results = Route.ArrayToTypedArray( e.result as ArrayCollection );
                              dispatchEvent( new RoutesEvent( RoutesEvent.LOAD, results ) );
                        case "devices":
                              trace( "got device results" );
                              results = Device.ArrayToTypedArray( e.result as ArrayCollection );
                              dispatchEvent( new DeviceEvent( DeviceEvent.LOAD, results ) );

On my user form, I call the init() function on creationComplete:

private function init():void {
      mySvc.getInstance().addEventListener( ShipmentsEvent.LOAD, GetShipments_Result );
      mySvc.getInstance().addEventListener( RoutesEvent.LOAD, GetRoutes_Result );
      mySvc.getInstance().addEventListener( DeviceEvent.LOAD, GetDevices_Result );

      mySvc.getInstance().getShipments( 2 );
      mySvc.getInstance().getRoutes( 2 );
      mySvc.getInstance().getDevices( 2 );

In Service Capture, as expected I see 4 requests to the web service and the correct results returned:
      GET WSDL

However, only one trace gets fired...
"got shipment results"

If I sequence them by calling getRoutes in the result handler of getShipments, and getDevices in the result handler of getRoutes, all three events are received fine.

However, this seems horribly clumsy and hard to manage over a large enterprise application.

What might I be doing wrong, or is sequencing the only way to go?
Question by:FredMBarrett
  • 5
  • 3
LVL 20

Expert Comment

ID: 24098827

Could you try to add a trace-statement outside the switch statement? I would guess that the there are problems with the token, but it's just an assumption, as I was having similar problems.

Also I would try to change the callback signature to Event for debugging, because when using the different types of registering callbacks I had different types of events fired.

I would recomend adding the event listener to the token, that you are recieving or to the method. You are currently registering to events fired by the webservice itself.

var _getShipmentsToken:AsyncToken;

public function GetShipments( custId:int ):void {
    getShipmentsToken = ws.getShipmentsForCustomer( custId );
    getShipmentsToken.addEventListener( "result", WebService_Results );
    getShipmentsToken.marker = "shipments";

Notice the token is defined outside the function scope because I had problems otherwise. I guess as soon as flex leaves the function, the local variables are destroyed and so is the token.

Or (I am using the Flex Builder Webservice Adapters):

public function GetShipments( custId:int ):void {
    var callToken:AsyncToken = ws.getShipmentsForCustomer( custId );
    callToken.marker = "shipments";

Hope this helps,

Author Comment

ID: 24099340
Hi ChristoferDutz:

Thanks for your reply. I made the updates you suggested and still no love. The traces all show that, basically whichever event happens first, that one wins and all others are lost.

I moved the token definitions outside of the function and created a token for each method call.
I also added the event listener to the token itself.

            public function GetShipments( custId:int ):void {
                  ws.addEventListener( ResultEvent.RESULT, WebService_Results );
                  _getShipmentsToken = ws.getShipmentsForCustomer( custId );
                  _getShipmentsToken.addEventListener( ResultEvent.RESULT, WebService_Results );
                  _getShipmentsToken.marker = ShipmentsEvent.LOAD;

Again, Service Capture shows the results coming in, and they all do come in correctly, but whichever result appears first is the only one that bubbles back and none of the other events get fired.

Very frustrating as you can imagine.
LVL 20

Expert Comment

ID: 24100822
Have you turned on the logging capabilities of flex?
When im in my office again tomorrow, I could send you a code fragment to initialize the Flex log-framework. It helped me quite a lot when having similar problems.

Are you using the flexBuilder generated Webservice-client?

Whatever you are using, I would suggest setting a breakpoint in your event handler (The one that is called), step up one layer and have a look at the code there. When figguring out my problems regarding Flex + CXF + Aegis integration I had to do quite a lot of debugging in the Flex sources :-(

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 20

Assisted Solution

ChristoferDutz earned 1000 total points
ID: 24106093
I call the following code to get a lot of debug information on my console (When debugging flex using the Flex Builder and Eclipse):
	    	private function initLogging():void 
	            // Create a target.
	            var logTarget:TraceTarget = new TraceTarget();
	            // Log only messages for the classes in the mx.rpc.* and 
	            // mx.messaging packages.
	            // Log all log levels.
	            logTarget.level = LogEventLevel.ALL;
	            // Add date, time, category, and log level to the output.
	            logTarget.includeDate = true;
	            logTarget.includeTime = true;
	            logTarget.includeCategory = true;
	            logTarget.includeLevel = true;
	            // Begin logging.

Open in new window


Author Comment

ID: 24109144
Thank you, Christofer...
Where does this log file get generated?
LVL 20

Expert Comment

ID: 24138481
Well the way it is configured in the sample I posted, it is generated in the Eclipse console of my Flex Builder. You can however let the debug version of the Flash player debug to a local file:

Here the link for the Debug version of the flash player:

Accepted Solution

FredMBarrett earned 0 total points
ID: 24150123
Well, I found the problem through experimentation, and I am chalking it up to "not all good examples extend to enterprise-level solutions."

As you can see from the first example in the list, each function added its own listener to the event. The correct solution is to add a single listener at the module scope.

Each caller function would .addEventListener and the handler would .removeEventListener, and with many of these going on throughout the processing, events would get lost.

By adding a single event listener in the creationComplete() function, and then not removing it as part of the handler functions, I haven't lost any events since, and I can call multiple web service methods in a row and get all the results I expect.

Now it seems obvious, but without Service Capture and help from ChrisoferDutz, it was quite maddening!

Thanks, Chris.
private function MyService_Init():void {
   ws = new WebService();
   ws.addEventListener( ResultEvent.RESULT, MyService_Results );
   ... //other init stuff
public function GetShipments( custId:int ):void {
   var token:AsyncToken = ws.getShipmentsForCustomer( custId );
   token.marker = ShipmentsEvent.LOAD;
private function MyService_Results( event:ResultEvent ):void {
   var token:AsyncToken = event.token;
   var resultsArray:ArrayCollection;
   var resultsXML:XML;
   if ( token != null ) {
      trace( token.marker );
      switch ( token.marker ) {
      case UserLoginEvent.GET_USER:
         resultsArray = UserProfile.ArrayToTypedArray( 
            event.result as ArrayCollection );
         dispatchEvent( new UserLoginEvent( resultsArray ) );
      case ShipmentsEvent.LOAD:
         resultsArray = Shipment.ArrayToTypedArray( 
            event.result as ArrayCollection );
         dispatchEvent( new ShipmentsEvent( ShipmentsEvent.LOAD, resultsArray ) );

Open in new window

LVL 20

Expert Comment

ID: 24155561
Ok ... the detail with the removeListener wasn't visible in your example ... Seems that this indeed seems to be the cause of your problems :-)

I'd suggest you close the question and don't add it to the knowledge-base.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question