Apache – Programming synchronous web service calls in flex

apache-flex

Web service calls are asynchronous in flex, but I wanted to wrap a web service call in a class to provide synchronous encapsulation. Something like the below – the user could call getMyMethodResult and get the result returned by the web service. I expected the thread that recieved the soap response would populate the variable _result and mean that getMyMethod would, after a time, find _result is not longer null. But it doesn't! Can anyone explain why this does not work?

public class myClass
{   
    private var _result:Object;
    public function myClass()
    {
        //create a web service object
        ...

        // Add listener
        _service.addMyMethodListener(myMethodListener);
    }

    public function getMyMethodResult()
    {
        _service.myMethod();

        while (_result == null)
        {
        // count a variable or something (unimportant)
        }

        return _result;
    }

    private function myMethodListener(event:Event):void
    {
        _result = event.result;
    }
}

Best Answer

There's is absolutely no support for that. The event loop runs between frames and as long as you block the execution with your (infinite) loop, your myMethodListener function will not be called. Anyway, this would be a terrible idea since the absence of threading in the Flash Player will cause your UI to freeze while you wait for your service to return. You should just drop that idea.

Related Topic