Best approach to wait untill all service calls returned values in Flex PureMVC

352 Views Asked by At

I am writing an Adobe AIR application using PureMVC.

Imagine that I have an page-based application view ( using ViewStack ), and user is navigating through this pages in some way ( like clicking the button or whatever ).

Now for example I have an Account Infromation page which when instantiated or showed again needs to load the data from WebService ( for example email, account balance and username ), and when the data is returned I want to show it on my Account Information page in the proper labels.

The problem is when I will execute this three Web Calls, each of them will return different resultEvent at different time. I am wondering what is the best way to get the information that ALL of the service calls returned results, so I know that I can finally show all the results at once ( and maybe before this happens play some loading screen ).

2

There are 2 best solutions below

0
T. Richter On

I really don't know much about PureMVC, but the as3commons-async library is great for managing async calls and should work just fine in any framework-setup

http://as3commons.org/as3-commons-async/

In your case, you could create 3 classes implementing IOperation or IAsyncCommand (depending on if you plan to execute the operations immediately or deferred) encapsulating your RPCs. After that is done you simply create a new CompositeCommand and add the operations to its queue. When all is done, CompositeCommand will fire an OperationEvent.COMPLETE

BTW, the library even includes some pre-implemented common Flex Operations, such as HTTPRequest, when you download the as3commons-asyc-flex package as well.

1
Anton On

I would do it in this way:

  1. Create a proxy for each of three information entities (EMailProxy, BalanceProxy, UsernameProxy);
  2. Create a delegate class which handles the interaction with your WebService (something like "public class WSConnector implements IResponder{...}"), which is used by the proxies to call the end ws-methods;
  3. Create a proxy which coordinates all the three results (CoordProxy);
  4. Choose a mediator which will coordinate all the three calls (for example it could be done by your ApplicationMediator);
  5. Create notification constants for all proxy results (GET_EMAIL_RESULT, GET_BALANCE_RESULT, GET_USERNAME_RESULT, COORD_RESULT);
  6. Let the ApplicationMediator get all 4 notifications;

    • it is important that you should not only wait for all three results but also be ready for some errors and their interpretation. That is why a simple counter could be too weak.

The overall workflow could look like this:

  1. The user initiates the process;
  2. Some mediator gets an event from your GUI-component and sends a notification like DO_TRIPLECALL;
  3. The ApplicationMediator catches this notification, drops the state of the CoordProxy and calls all 3 methods from your proxies (getEMail, getBalance, getUsername).
  4. The responses are coming asynchronously. Each proxy gets its response from the delegate, changes its own data object and sends an appropriate notification.
  5. The ApplicationMediator catches those notifications and changes the state of the CoordProxy. When all three responses are there (may be not all are successful) the CoordProxy sends a notification with the overall result.

I know it is not the best approach to do such an interaction through mediators. The initial idea was to use commands for all "business logic" decisions. But it can be too boring to create the bureaucracy.

I hope it can help you. I would be glad to know your solution and discuss it here.