Marco Aurélio Deleu
Marco Aurélio Deleu

Reputation: 4367

Problems with multi-task on Flex

I have a method that calls two services from PHP at the same time. Due to multi-tasking abilities of Flex, I think that each service is called in a different thread.

My problem is: Both services return an Array of Objects from the database. But the second service will feed a DataGrid that has a handler for each record. This handler will compare that from both Arrays and when Flex finishes the second one before finishes the first one, I have a problem because the handler tries to compare data with a null object (the PHP Service didn't respond yet).

Any ideas?

EDIT:

On the day that I posted this question, some guy gave me an amazing idea, but sadly seems like he deleted his post, I don't know why. I kept his idea on my mind and I found a solution that fits my design pattern with his idea.

He told me to put a flag telling me if the data was already loaded or not. So here is what I'm doing now: I call first service; I call second service;

On the result of first service, I check the Flag on the second service, if it's true, it means that it was already loaded, so I can just store my data in the DataGrid so the handler can be called. If the flag is false, it means that the second data wasn't loaded yet, so instead of storage the data in the official dataProvider, I storage it on a _temp DataProvider that is not BOUND to the dataGrid. In this case, when the second data is loaded, a listener event is dispatched to the first service telling him to catch the _temp dataProvider and copy that to the official dataProvider.

Particularly, I liked the solution and it doesn't break the Table Data Gateway design pattern.

Thanks everyone for the help.

Upvotes: 0

Views: 126

Answers (3)

weltraumpirat
weltraumpirat

Reputation: 22604

Due to multi-tasking abilities of Flex, I think that each service is called in a different thread.

What makes you think Flex supports multi-threading? It really doesn't. Single Thread only.

However, your calls are asynchronous in that when they are sent, the program does not stop to wait for an answer, but listens for a completion event.

Your solution is simple: Send only the first request, wait for it to complete, and then send the second request in the completion handler.

EDIT

To preserve your design pattern, you can apply two different strategies:

  1. Use the Business Delegate pattern: Have a custom class act as the gateway and encapsulate the actual connections to your services. It could then dispatch its own COMPLETE event to trigger the handlers you have. To the DataGrid, it would appear like a single asynchronous call.

  2. Use the Session Facade pattern: Create a single service on the server side, which accepts a single request, calls the referenced services locally, and returns the combined data for both services in a single response.

Upvotes: 5

Angelo
Angelo

Reputation: 1666

Let us assume you have two services.. FirstService SecondService

private function init(): void
{
  // Call the first service.
  myService.FirstService();
}

private function firstServiceResult(re:ResultEvent) :void
{
  // Perform what you need to do with the results of your FirstService (i.e. set the result to array).
  // Afterwards, call the next service.
  myService.SecondService();
}

private function secondServiceResult(re:ResultEvent) :void
{
  // Perform what you need to do with the results of your SecondService.
  // Now you can compare the result in the first service and the second service.
}

Upvotes: 0

Dan Monego
Dan Monego

Reputation: 10117

Flex doesn't have multi threading, but it can have multiple asynchronous calls at once. You can deal with not knowing which will return first by having each return handler check to make sure that both services have returned before progressing into code that depends on both.

Upvotes: 1

Related Questions