1 Reply Latest reply on Aug 3, 2009 10:51 PM by Gregory Lafrance

    Air Application Save / Load ArrayCollection

    epi3rc3 Level 1

      i have an air application with a ArrayCollection filled with Strings and i was wondering what would be the best method( easiest ) to save this ArrayCollection to a file when application is closed and reopen the saved ArrayCollection when it starts. i was thinking that saving it to a txt file would be the best way since someone could just go into the txt and modify it if they wanted but from looking at code around the net i dont see how i could do this with an ArrayCollection. any ideas?

        • 1. Re: Air Application Save / Load ArrayCollection
          Gregory Lafrance Level 6

          If this post answers your question or helps, please mark it as such.

           

          You can convert an ArrayCollection to a string with ac.toString().

           

          You can convert a string to an ArrayCollection with mx.utils.ArrayUtil.toArray()

           

          The following code saves the ArrayCollection to SharedObject, and as long as the data will be less than 100K this may be the way to go, otherwise see this link on writing files, and then use toString() and ArrayUtil.toArray():

           

          http://help.adobe.com/en_US/AIR/1.5/devappsflex/WS5b3ccc516d4fbf351e63e3d118666ade46-7dc2. html

           

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
            creationComplete="init();">
            <mx:Script>
              <![CDATA[
                import flash.net.SharedObject;
                import mx.collections.ArrayCollection;
                import mx.utils.ArrayUtil;
               
                private var sharedObj:SharedObject;
                private var ac:ArrayCollection;
               
                private var soAC:ArrayCollection;

                private function init():void{
                  ac = new ArrayCollection(["one", "two", "three", "four"]);
                  sharedObj = SharedObject.getLocal("ac");
                  sharedObj.data.ac = ac.toString();
                  sharedObj.flush();
                  sharedObj = SharedObject.getLocal("ac");
                  if (sharedObj.size > 0){
                    trace("sharedObj: ");
                    soAC = new ArrayCollection(ArrayUtil.toArray(sharedObj.data.ac));
                    trace(soAC.toString());
                  }
                }
              ]]>
            </mx:Script>
          </mx:Application>