what is best way to write dispatch events in this Service class function?

kimcity's Avatar

kimcity

25 May, 2012 10:44 AM

I have the following function in a Service class

protected function writeToFile():void{
        var destinationFile:File = File.applicationStorageDirectory.resolvePath(_newPath);

        var fs:FileStream = new FileStream();

        fs.open(destinationFile, FileMode.WRITE);
        fs.writeBytes(_buffer, 0, _buffer.length);

        fs.close();         


    }

this Service class should dispatch a SuccessfulCreateLocalFileEvent when writeToFile is successful and UnsuccessfulCreateLocalFileEvent if writeToFile is unsuccessful

I know that FileStream functions open and writeBytes may throw IOError if there is any problems. And all these FileStream functions all have void as return types.

I believe I need try catch, but I am not entirely sure how to do so.

Please advise.

This question is replicated at http://codereview.stackexchange.com/questions/12061/add-events-disp... Experimenting which site has better answers for such a question.

  1. 2 Posted by krasimir on 25 May, 2012 11:08 AM

    krasimir's Avatar

    Hello,

    FileSteram class has its own events which are dispatched when the file operation is successful or not.
    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/...

    Isn't it ok to use that.

  2. 3 Posted by kimcity on 26 May, 2012 02:05 AM

    kimcity's Avatar

    Hi krasimir,

    you mean like this?

        protected function writeToFile():void{
            _destinationFile = File.applicationStorageDirectory.resolvePath(_newPath);
    
            var fs:FileStream = new FileStream();
    
            fs.open(_destinationFile, FileMode.WRITE);
            fs.writeBytes(_buffer, 0, _buffer.length);
    
            fs.addEventListener(Event.CLOSE, dispatchFileCreatedEvent);
    
            fs.close(); 
    
        }
    
  3. 4 Posted by krasimir on 26 May, 2012 03:33 PM

    krasimir's Avatar

    Hi, yes. Exactly that thing.

  4. 5 Posted by kimcity on 26 May, 2012 06:41 PM

    kimcity's Avatar

    Hi krasimir,

    thank you!

  5. kimcity closed this discussion on 26 May, 2012 06:41 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac