Best Practice for callbacks?

Raed Atoui's Avatar

Raed Atoui

27 Apr, 2010 09:25 PM

In this previous [discussion] (
it was determined that it was a bad practive to pass the model along events. Yet in the Flickr demo from the demo bundle, a VO is being passed in an event from the Mediator to a Command that processes that same VO.

Lets say you have a hierarchical data structure where you load each depth of the tree using configurable parameters. At each stage, you need to append the results to a given VO, and repeat until you reach the leaves. And for siblings, fetching children nodes must happen in parallel.
I dont see why CRUD commands cant have a reference to the VOs.

Also, i have been following the AyncCommand discussion and chaining commands, but it would be nice to see a sample working example if a consensus is reached?

  1. 1 Posted by Raed Atoui on 28 Apr, 2010 01:38 PM

    Raed Atoui's Avatar

    ok for the first, i got it, sorry. The previous post was talking about "Model" as in the model managers.

    Can anyone please tell me if this makes sense?


        public var service:IService;
        public var event:Event;
        override public function execute():void {
                public function callBack(results:Object):void { .. 
                     //process results
                   //dispatch further events from here

    And in the service side (IService)

              public function execute(someobject:Object,callback:Function):void {
                   //call a remote service
                 //add result handler
                token = service.send();
                token.callBack = callBack;
             private function onResult(event:ResultEvent):void {
                     var token:AsyncToken = event.token;

    This actually worked, but it feels so so to me. Is that Command object kept in memory now?

  2. Support Staff 2 Posted by Shaun Smith on 28 Apr, 2010 01:54 PM

    Shaun Smith's Avatar

    That makes sense. The Command object will be kept in memory as long as you keep a reference to the token/callback (I can't tell if the token in Service#execute() is held on to after the result is issued or not).

  3. 3 Posted by Raed Atoui on 28 Apr, 2010 02:03 PM

    Raed Atoui's Avatar

    thank you for you reply.

    I guess i failed to mention that the results in the callback are passed to the Model.
    Ideally, after the results are returned to the command, processed and passed to the Model, i want to dispose of the command entirely.
    so would the AsyncCommand alleviate this problem of memory references?


  4. Support Staff 4 Posted by Shaun Smith on 28 Apr, 2010 08:58 PM

    Shaun Smith's Avatar

    You don't need an AsyncCommand in your particular case, as the callback reference will keep the Command alive until you let go of it. As far as I can see, your Command should be free for GC as soon as the callback is executed.

    You can step through it like this:

    1. Command is executed, passes callback reference to service.
    2. Service holds on to callback reference (and thus the Command instance) by adding it to a token.
    3. Service result handler fires, executes the callback, and lets go of the token (and thus the callback, and thus the Command instance).

    As long as you aren't holding on to that token (or the callback) there will be no references left that point to the Command instance. It should be free for GC, and removed when the Flash Player needs more memory (or whenever it feels like it - it's not predictable).

  5. 5 Posted by Raed Atoui on 29 Apr, 2010 04:21 AM

    Raed Atoui's Avatar

    yes, the GC is not predictable, but that approach should be fine as long as the commands get discarded.

    thank you so much for responding and you can close this thread now


  6. Shaun Smith closed this discussion on 29 Apr, 2010 01:18 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac