A friend of mine asked me the difference between Synchronous and Asynchronous file operations in AIR. So here I will try to explain for all that are interested.
A FileStream object is used to read and write files. Files can be opened synchronously by calling the open() method or asynchronously by calling the openAsync() method.
Synchronous File operations:
The benefit of using sync file operations is that it uses far less code to complete a file load and should actually only be used on small files. The rule of thumb I use is < 1MB.
import flash.filesystem.*;
var file:File = File.documentsDirectory;
file = file.resolvePath("someFile.txt");
var fs:FileStream = new FileStream();
fs.open(file, FileMode.READ);
var data:String = fs.readUTFBytes(fs.bytesAvailable, File.systemCharset);
fs.close();
When a file is loaded in in an sync matter the rest of the application is halted/ it freezes until the complete file is loaded in. This is one of the reasons why you should only use this on small files.
Aynchronous File operations:
The advantage of opening files asynchronously is that other code still runs while in the background while the read and write operations are done. When opened asynchronously, progress events are dispatched as operations proceed.
It uses more code to handle a loading of an file:
import flash.filesystem.*;
var file:File = File.documentsDirectory;
file = file.resolvePath("someFile.txt");
var fs:FileStream = new FileStream();
fs.addEventListener(Event.COMPLETE, onComplete);
fs.addEventListener(ProgressEvent.PROGRESS, onFileProgress);
fs.addEventListener(ProgressEvent.OUTPUT_PROGRESS, onFileOutputProgress);
fs.open(file, FileMode.READ);
function onComplete( e:Event ):void {
var data:String = fs.readUTFBytes(fs.bytesAvailable, File.systemCharset);
fs.close();
}
function onFileProgress( e:ProgressEvent):void {
// You can show a loader while its still processing
// Signals the availability of new data on the stream.
trace(e.bytesLoaded);
}
function onFileOutputProgress( e:ProgressEvent):void {
// You can show a loader while its still processing
// Signals that buffered data has been written to the file.
trace(e.bytesTotal, e.bytesPending);
}
For prototyping synchronous is the way to go, because it is fast and not used in real life situations. If you have really big files you are loading in, FLV files that are 500 MB or other huge files, asynchronous is your best option.
I hope this clears things up about the difference in loading files in different manners. Comments are always welcome!




on Jan 15th, 2009 at 10:23 am
Have you noticed that Asyncronous file write commands don’t dispatch progress events? They do it for read commands, but not for write!
on Sep 19th, 2009 at 7:50 am
@James (and others): You need to register to outputProgress Events.
on Sep 19th, 2009 at 10:22 am
Thanks florian! Also updated the example above
on Mar 20th, 2010 at 9:41 am
[...] used openAsync instead of open because I wanted the other code to run at the same time while the images were being copied in the background. Plus, since I used Robert Penner’s AS3Signals as my main event mechanism throughout this [...]