fzhou
fzhou

Reputation: 417

Asynchronous Lazy Loading in ActionScript

I am having trouble with implementing this. Basically, I have many large chunks of data that need to load with URLoader. And I want to use lazy loading because eager loading takes up a lot of network resources/time. For example:

class Foo {
    private var _resources:Array;
    public get_resource(id:String){
            if ( _resources[id] == null ){
                 //Load the resource
            }
            return _resources[id];
    }
 }

However, the above code only works with synchronous loading. I don't want the asynchronous being exposed to other parts of the program. How do I do this? Thanks.

Upvotes: 0

Views: 1248

Answers (2)

back2dos
back2dos

Reputation: 15623

I'm afraid you can't ... flash player is single threaded (at least for nearly everything that counts), which is why all I/O operations are asynchronous.

the solution presented by torus is about the best you can get.

Upvotes: 0

torus
torus

Reputation: 1049

Since an asynchronous function returns immediately (before loading is done), you need to pass a callback function (event handler) to get data from it.

The code may be something like this:

class Foo {
    private var _resources:Array;
    private var _loader:Array; // holds URLLoader objects
    public get_resource(id:String, callback:Function) : void {
        if ( _loader[id] == null ){
            var ld : URLLoader = new URLLoader();

            // instead of returning value, callback function will be
            // called with retrieved data
            ld.addEventListener(Event.COMPLETE, function (ev:Event) : void {
                    _resources[id] = ev.target.data;
                    callback(ev.target.data);
                });

            ld.load (/* URLRequest here */);

            _loader[id] = ld;
        }
        // return nothing for now
    }
}

[edit] removed a wrong comment "// URLLoader object must be held by a class variable to avoid GC". But _loader is still needed for remembering which id is loading.

Upvotes: 3

Related Questions