All entries for Tuesday 04 November 2008

November 04, 2008

AIR FileReference and scope

I ran into a small problem recently in my AIR application when using FileReference to initiate and monitor a download operation; no events from a FileReference object created within a method were being fired. After a quick check of the Livedoc entry I found the issue:

if the FileReference object goes out of scope, any upload or download that is not yet completed on that object is canceled upon leaving the scope. Be sure that your FileReference object remains in scope for as long as the upload or download is expected to continue.

So basically a FileReference object needs to be created in such a way that it stays in scope for the duration of your operation; if you instantiate it within a method it there’s a good chance it will go out of scope and any events you were waiting for from it will never fire. The easiest solution is to declare it outside of the method, so instead of:


private function download(event:MouseEvent):void 
{
    var fileRef:FileReference = new FileReference();
    fileRef.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
    fileRef.addEventListener(Event.COMPLETE, onComplete);
    //etc.
}
you need to do something like this:

private var fileRef:FileReference();
private function download(event:MouseEvent):void 
{
    fileRef = new FileReference();
    fileRef.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
    fileRef.addEventListener(Event.COMPLETE, onComplete);
    //etc.
}

Sorted. Afterwards I also found more info about this via a couple of useful blog articles here and here.


Search:

MXNA link

Tweets



    Tags

    Other blogs I like...

    Black Pepper Software

    Emak Mafu Go to 'Comments on: '

    Eismann-sf Go to 'Comments on: Design News for Web, Graphic Designers'

    Ted On Flex Go to 'Ted On Flash'

    Galleries

    Meetups:

    Not signed in
    Sign in

    Powered by BlogBuilder
    © MMXVII