I’m confused about the proper way to save a Local Shared Object. There are those who say the data is automatically saved. There are those who say you should not count on that but use the SharedObject.flush() method. And I’ve heard that when you’re done you should call the close() method or you will not be able to use a SharedObject again later.
The problem is, if I have my local storage settings to “ask,” the flush() call will invoke the settings dialog, and the close() call invokes it again, so I have to click Allow (or Deny) twice.
Any enlightenment on this?
public function save(solName:String, propName:String, propValue:*):void{
so = SharedObject.getLocal(solName);
so.data[propName] = propValue;
try{var flushResult:String = so.flush();
if (flushResult == SharedObjectFlushStatus.FLUSHED){
dispatchEvent(new Event(SAVE_SUCCESSFUL));
//so.close(); <--- WOULD OPEN SETTINGS DIALOG A SECOND TIME
}else if (flushResult == SharedObjectFlushStatus.PENDING){
so.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
}
}catch(e:Error){
// user has local storage settings to "Never"
dispatchEvent(new Event(LOCAL_STORAGE_NEVER));
//so.close();
}
private function onNetStatus(e:NetStatusEvent):void{
if (e.info.code == "SharedObject.Flush.Success"){
dispatchEvent(new Event(SAVE_SUCCESSFUL));
}else if (e.info.code == "SharedObject.Flush.Failed"){
dispatchEvent(new Event(LOCAL_STORAGE_DENIED));
}
//so.close();
//so.removeEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
//and can't remove that event listener if I'm calling close()
//because I'll get an unhandled NetStatusEvent error
}