The FileManager was written to run FileManagerTasks which run FileLoaders. The FileManager keeps a queue of tasks waiting to run and a list of the tasks running. The FileManager will not run another task if there are too many already running. It also won’t run a task if we’re already loading something with the same name or ID.
The FileLoader class checks first to see if we have the file on disk. If not, the file is downloaded and put on disk.
The FileLoader returns either the content as an array of bytes (or a UTF8 string), or it loads the content using the Flash Loader in the case of images and SWF files.
So here’s the rub: If you ask FileManager for a file and then ask for it again later and the original is still in memory, then why not just create a new DisplayObject from the one already in memory. For instance, if you’ve loaded an image, and you need another of that image, then you should just create a Bitmap from the BitmapData of the Bitmap already in the display list.
AND, SO, FINALLY, FileManager was upgraded to keep a Dictionary whose keys are weak references to the BitmapData references of the Bitmap DisplayObjects that were loaded.
In case you’ve found this blog post because you’re wanting or think you’re wanting to mess with weak references, then I’ll put a snipped of code below, but I suggest you read the following before you panic yourself into thinking you always need to use weak references.
ActionScript 3.0 Events: The Myth of useWeakReference
protected static var loadedBitmaps:Dictionary = new Dictionary(true);
if ( task.loader.content is Bitmap )
{
var bitmap:Bitmap = task.loader.content as Bitmap;
loadedBitmaps[bitmap.bitmapData] = task.id_or_filename.toString();
}
for ( var ref:* in loadedBitmaps )
{
if ( loadedBitmaps[ref] == task.id_or_filename.toString() )
{
var event:FileManagerCallbackEvent = new FileManagerCallbackEvent();
event.id_or_filename = task.id_or_filename;
event.status = FileManagerCallbackEvent.STATUS_COMPLETE;
event.content = new Bitmap( ref as BitmapData );
task.callback(event);
processQueue(); // See if there's anything left to do.
return;
}
}
Leave a Reply