0

Let's say that I have a NetConnection Object inside a Singleton Pattern that is shared with all Data Access Object classes throughout all my application. Now, you went on a screen and asked to load 5 thousands of records because you want to search for some mistake or whatever. But now you just realized that you're in the wrong screen and you actually wants 5 thousand of records from another "Entity". How to prevent you from having to wait almost 2 minutes until the data gets here so you can move on to another Screen and ask for some different data? I tried using ESC button as a key caller to the NetConnection.close() event, but it actually doesn't work. It just dispatch an empty event and the call still returns. I know that I can't ask the server not to keep doing his job, but I wanna know how I can make sure that after you pressed ESC or something like that, the ActionScript won't bother with that specific call anymore and you can move on with your life. And, when the server respond to that call, in the background, I'm going to throw it away because you already agreed to cancel it.

Ps.: I don't have any doubt on how to listen to ESC button and call some method through it, I just want to know what to call once the user decided that he doesn't want to receive that data anymore and he/she wants to move on without having to close the App and open again.

Edit:

I tried to destroy the Responder object after making the call, but it didn't work.

        public function call(command:String, f:Function, fault:Function, ...parameters):void{
        var r:Responder = new Responder(f, fault);
        this._Responder = r;
        if(parameters.length == 0)
            GATEWAY.call(command, _Responder);
        else
            GATEWAY.call(command, _Responder, parameters);
    }

    public function close():void{           
        _Responder = new Responder(function(obj:Object):void{}, function(obj:Object):void{});
        //_Responder = null;
        trace("Canceled");
    }

with the code described up here, I stopped making calls to NetConnection::call and started to call the "Call Function" that I wrote. So this function would build a Responder that will be in the class scope so I could access it even after the call has been made and the ESC button would call the "Close" function. I tried setting it to null and re-building it. It didn't work. I think after the NetConnection makes the call, the Responder cannot be changed.

Marco Aurélio Deleu
  • 4,279
  • 4
  • 35
  • 63
  • Is there a reason you have to use the singleton connection or some benefit you're looking to gain from this? Regardless my thought is to setup a cancel method in the singleton that you call, in the cancel method you create a new NetConnection and deactivate any handlers you had in place (honestly haven't used NetConnection directly myself much but we've used the AbstractService RPC classes and IResponder implementing objects to achieve similar tasks, though generally we actually have an instance of the service class for each of our Java services, not each method but each service class). – shaunhusain May 28 '12 at 04:37
  • I tried calling `NetConnection::close`, rebuilding the `NetConnection` class and setting the object to null. I also tried what I described in the edit just now, that was setting null not only to the NetConnection Object, but also for the Responder Object. Nothing seems to work. – Marco Aurélio Deleu May 28 '12 at 17:06

1 Answers1

0

The NetConnection class does not have support for cancelling AMF calls. However, like shaunhusain said, you can keep storage (array, dictionary whatever) of the methods being called from your singleton class and once you cancel a call, it should disable the result event listeners for that call only.

So, even if you exit the module that is waiting for the result from the AmfCall, there will be no callbacks from your singleton class and you can continue to the next screen.

public class Call
{
var responder:Responder;
var service:String;
var params:Array;

public function Call(service:String, params:Array)
{
   responder = new Responder(this.onResult, this.onFault);
   this.service = service;
   this.params = params;

}

public function removeListeners():void
{
   removeEventListener(CallEvent.RESULT, onResult);
}

public function onResult(result:Object):void
{
   this.result = result;
   dispatchEvent(new CallEvent(CallEvent.RESULT, callId, result));
}

}

The Call class is responsible for dispatching events on receiving a result. However, if there are no event listeners, the events dispatched will not be listened by any class.

This code snippet is taken from AmfPhpToolBox. You might want to use that library instead to tackle all these issues.

AMFPHPToolBox

Mohammad Haseeb
  • 429
  • 3
  • 11
  • I don't think I can do that with NetConnection class. – Marco Aurélio Deleu May 28 '12 at 17:00
  • Like I said, you can't do that with the NetConnection class, instead you need to keep a call class that has its own result events or callback functions. Whenever you make a call through your singleton class, enqueue the call in to your local storage and setup up the result event listeners. Once the call is processed, dispatch the result event. – Mohammad Haseeb May 30 '12 at 06:26