FileStream的错误hanlder - AS3 AIR

时间:2015-04-24 04:37:33

标签: actionscript-3 flash air filestream

我使用FileStream将对象保存到文件中。我保存的对象中包含变量:

var saveObj:Object = new Object()
saveObj['FirstName']="Georges"
saveObj['LastName']="St-Pierre"

我的问题是:如何在加载文件时为变量创建错误处理程序?例如'名字'不见了。我怎么做?这是我的代码,但它没有用。

private function F_loadData():void {
    folder = File.documentsDirectory.resolvePath("saved projects");
    var file:File = folder.resolvePath("mySave.adktf");
    var stream:FileStream = new FileStream();
    stream.addEventListener(Event.COMPLETE, completeHandler); 
    stream.addEventListener(ProgressEvent.PROGRESS, progressHandler); 
    stream.addEventListener(IOErrorEvent.IO_ERROR, errorHandler); 
    stream.openAsync(file, FileMode.READ);
    var myLoadedObj:Object = stream.readObject(); 
    stream.close()
    trace(myLoadedObj.MiddleName)
}
private function completeHandler(event:Event):void { 
    trace("load Complete")//not final. Just for testing
} 

private function progressHandler(event:ProgressEvent):void { 
    // ... 
} 
//I was hoping the error would call this function
private function errorHandler(event:IOErrorEvent):void { 
    trace("Error found")//not final. Just for testing
} 

trace(myLoadedObj.MiddleName)应该返回一个错误,我希望它会调用我所做的错误处理程序函数,但它没有。它仍然会返回错误。那么我应该使用哪种类型的错误处理程序?

1 个答案:

答案 0 :(得分:2)

由于您是异步打开文件,因此需要等到完成事件之后再使用readObject

您定义的错误处理程序仅适用于加载文件。因此,如果打开文件时出现问题,将调用错误处理程序,任何其他类型的错误都在该范围之外。

尝试这样的事情:

private var stream:FileStream;
private function F_loadData():void {
    folder = File.documentsDirectory.resolvePath("saved projects");
    var file:File = folder.resolvePath("mySave.adktf");

    if(file.exists){
        stream = new FileStream();
        stream.addEventListener(Event.COMPLETE, completeHandler); 
        stream.addEventListener(ProgressEvent.PROGRESS, progressHandler); 
        stream.addEventListener(IOErrorEvent.IO_ERROR, errorHandler); 
        stream.openAsync(file, FileMode.READ);
        //now wait for it to load
    }else{
        //file doesn't exist, do something
    }
}
private function completeHandler(e:Event):void { 
    trace("load Complete")//not final. Just for testing

    var myLoadedObj:Object = stream.readObject(); 
    stream.close();
    stream = null;

    if(!myLoadedObj.MiddleName || myLoadedObj.MiddleName == ""){
        //Middle name is missing or empty, do something
    }

    if(!myLoadedObj.FirstName || myLoadedObj.FirstName == ""){
       //First name is missing or empty, do something
    }
} 

您可能更适合为保存对象使用自定义类,如下所示:

package {
    public class SaveObject {
        public var middleName:String;
        public var lastName:String;
        //any other properties

        public function validate():Boolean {
            //write some validation

            //if you want lazy validation, you could do this: (untested)
            for (var i in obj){
                if(obj[i] is String && (obj[i] == null || obj[i] == "")){
                    return false;
                }
            }
            return true;
        }
    }
}

然后,你可以这样做:(在你的构造函数中)

flash.net.registerClassAlias("SaveData",SaveData);

然后您可以执行以下操作:

var mySaveData:SaveData = new SaveData();
//set properties

stream.writeObject(mySaveData);


//then later
var mySaveData:SaveData = stream.readObject();

if(!mySaveData.validate()){
    //do something, it's invalid
}