1 Reply Latest reply on Aug 24, 2007 11:33 AM by krrobert

    SharedObject problems

    SPGAnne Level 1
      I am trying to store some XML in the SharedObject, but it doesn't seem to work. I think I'm getting it in there, but if I close the app and then try to retrieve it, it is null. I have the following test code. What the heck am I doing wrong?!?!?

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
      layout="absolute" creationComplete="init()">
      <mx:Script>
      <![CDATA[
      import valueObjects.CustomVO;
      import flash.net.registerClassAlias;


      private var mySO:SharedObject;
      private function init():void{
      mySO = SharedObject.getLocal("testSO");
      }

      private function saveSO():void{
      var obj:CustomVO = new CustomVO("test string",12345);
      var tagname:String = "mytag";
      var x:XML = <{tagname} name={obj.name} id={obj.id}/>;
      var test:CustomVO = CustomVO.buildCustomVOFromXML(x);
      trace (test); //this works fine, so my x:XML variable seems fine
      mySO.data.testString = obj.name;
      mySO.data.testXMLData = x;
      // the debugger shows the data is in mySO fine at this point
      mySO.flush();
      }
      private function getSO():void{
      //if I close the application and then launch again and invoke this
      //function the testString is there OK, but the testXMLData is null
      var theData:Object = mySO.data;
      var theXML:XML = mySO.data.testXMLData; //this comes back null
      trace(theData); // shows [object Object]
      trace(mySO.data.testString);// shows 'test string'
      trace("test xml data: "+theXML.toXMLString()); // shows no data
      }
      private function removeSO():void{
      mySO.clear();
      }
      ]]>
      </mx:Script>
      <mx:Button x="175" y="101" label="Save SharedObject" click="saveSO()"/>
      <mx:Button x="175" y="165" label="Retrieve SharedObject" click="getSO()"/>
      <mx:Button x="175" y="235" label="Remove SharedObject" click="removeSO()"/>

      </mx:Application>

      package valueObjects{
      [Bindable]
      public class CustomVO{
      public var name:String;
      public var id:Number;
      public var timeStamp:Number;

      public function CustomVO(name:String,id:Number){
      this.name = name;
      this.id = id;
      this.timeStamp = new Date().milliseconds;
      }
      public function toString():String{
      return ("[CustomVO]" +name+","+id+","+timeStamp);
      }
      public static function buildCustomVOFromXML(item:XML):CustomVO{
      var v:CustomVO = new CustomVO(item.@name, item.@id);

      return v;
      }
      }
      }