I have several proxys in my application that invoke to some remote objects. Different proxys can call to same operations in remote objects. I have defined a singleton wrapper service object which holds an instance of RemoteObject. The service exposes a getOperation() method that returns remote object operations. Proxys obtain references to the singleton service to obtain remote object operations. I've noticed that Flex RemoteObject.getOperation() method returns always the same operation object reference from RemoteObject, so new result and fault event listeners are added to existing ones. Then problem is multiple event listeners for same operation are been fired when operation is invoked, resulting in different proxy event listeners been executed. I only need one event listener to be fired (the one who is related to operation invoked in the proxy).
My remote service looks like:
private class RemoteService
{
protected static var _remoteService:Service = new RemoteService();
protected var _destination:String;
protected var _remoteObject:RemoteObject;
public function RemoteService(destination:String)
{
this._destination = destination;
this._remoteObject = new RemoteObject();
this._remoteObject.destination = destination;
}
public static function getInstance():RemoteService
{
return _remoteService;
}
public function getOperation(operationName:String, onResult:Function, onError:Function) : Operation {
var operation:Operation = _remoteObject.getOperation(operationName) as Operation;
operation.addEventListener(ResultEvent.RESULT, onResult);
operation.addEventListener(FaultEvent.FAULT, onError);
return operation;
}
}
An example of a proxy:
public class TestProxy
{
public static const NAME:String = "TestProxy";
protected var _remoteService:RemoteService;
public function TestProxy(){
super(NAME);
_remoteService = RemoteService.getInstance();
}
public function testOperation():void
{
var operation:Operation = _remoteService.getOperation("testOperationName", onTestResult, onTestFault);
operation.send();
}
...
Many proxys could have a reference to same RemoteService so when invoking testOperationName operation every event listener will be fired in different proxys.
I need a work around of this issue. I've thought of defining RemoteService as a Multiton instead of a Singleton, having one instance for each proxy. The I could have _remoteService = RemoteService.getInstance(PROXY_NAME) in each proxy. As each RemoteService has a reference to a diffent RemoteObject, operations will be unique too for each Proxy.
But then I will have many RemoteObject references (one for each proxy), with consecuent memory consumption (now I have only one RemoteObject reference).
Is this approach acceptable?
Thank you very much.