Anim8
Anim8

Reputation: 49

FileStream Events not being fired

To save and load character objects I am using File Stream in AIR, it is being targeted as an iPad app.

Event.complete is not firing and IOErrorEvent sometimes fires, I am at a loss.

Character class http://pastebin.com/pr6LSvMi

Thanks

-Anim8

Upvotes: 0

Views: 1181

Answers (1)

Josh
Josh

Reputation: 8149

Few things:

  1. Don't post your code on other sites. Post it directly in the question. This will ensure that the code is available for as long as StackOverflow is, whereas Pastebin could disappear tomorrow rendering this question useless in the archives
  2. In AS3, all object (including function) names should be lowerCaseCamelCase, class names should be UpperCaseCamelCase, constants should be UPPERCASE_UNDERSCORE_SEPARATED, and package names should be alllowercase. It won't cause any errors if you don't follow those, but they are the standard and we liked to point those out to devs here.
  3. You must always add your event listeners prior to the load for any object (be it URLLoader, Loader, FileStream, etc. Basically, there is a small chance that the load will finish prior to the event listener being added, meaning the event listener won't even fire (extremely uncommon, but I have seen it happen with disk reads). Additionally, do not call FileStream.close() until after the load has finished. The latter is, I believe, your problem. You are closing your connection before the connection has completed its load.

So, remove fsR.close() from

fsR.openAsync(fR, FileMode.READ);
fsR.addEventListener(Event.COMPLETE, LoadExistingCharacterObject);
fsR.addEventListener(IOErrorEvent.IO_ERROR, CreateNewCharacterObject);
fsR.close();

And add it to the handlers, LoadExistingCharacterObject and CreateNewCharacterObject. Additionally, you need to do the same in SaveCharacter. Any time you use FileStream.openAsync(), you cannot close the stream until after the load has completed or errored out. If you use FileStream.open(), you can close it immediately afterward since the application execution stops while the load is completed (FileStream.open() is a synchronous operation and FileStream.openAsync() is asynchronous)

You should read the LiveDocs for FileStream.

Upvotes: 4

Related Questions