Question

I'm having issues saving, then retrieving sharedObject data.

For example if I make a shared object:

var myData:SharedObject = SharedObject.getLocal("myData");

Then, I make an object, in this case, a "feathers" listcollection object

var lc:ListCollection = new ListCollection()

then assign it:

myData.data.lc = lc;

ok now, if i don't restart the phone (i'm working on android) and I assign the saved listCollection to listCollection var and it works fine like this:

var lc:ListCollection = myData.data.lc as ListCollection;

but if I restart the phone and do this:

var lc:ListCollection = myData.data.lc as ListCollection;

it won't recognize it as a "ListCollection" even if i cast it. It says that it was saved as an "Object"

So basically it's not saving the dataType through a restart. But if I load it before a restart it works...

Any ideas why SharedObject is not working? It works fine with typical objects. Is there a better way I can save data locally and retrieve it? WHy would it change the data type?

Was it helpful?

Solution

AS3 lang ref about SharedObject data:

Each attribute can be an object of any ActionScript or JavaScript type — Array, Number, Boolean, ByteArray, XML, and so on. For example, the following lines assign values to various aspects of a shared object:

Try to use ByteArray. How write object in SO:

            var list:ArrayList = new ArrayList(["test"]);
            var buffer:ByteArray = new ByteArray();
            buffer.writeObject(list);
            buffer.position = 0;

            var mySo:SharedObject = SharedObject.getLocal("application");
            mySo.data.list = buffer;
            mySo.flush();

And read object:

            var mySo:SharedObject = SharedObject.getLocal("application");
            var buffer:ByteArray = mySo.data.list;
            var result:ArrayList = ArrayList( buffer.readObject() );

For custom class, use registerClassAlias, see Vesper comment.

OTHER TIPS

Call registerClassAlias() prior to getting and setting shared object.

Manual

I do not believe SharedObject saves any type information, the best way to go around this is to create your own serialization deserialization process -- I believe SharedObject will do a naive deep iteration through the object, and try to serialize it.

It's probably a good idea to have a method that converts your objects as non-native implementations anyways, just in case you decide to use some other format to store your objects. With native JSON encoding/parsing -- there's no reason not to do it (plus, you can decide or not decide what to write) -- even if SharedObject did save class information, how would it determine private variables?

ie:

SerializableListCollection extends ListCollection {

  public function serialize():Object {
     var children:Array = [];

     // loop and add children
     children.push(child.serialize());

     return {
        children: children;
     }
  }
}

Then you can save JSON::stringify(ListCollection.serialize()) to save your data in a way that isn't tied to any implementation.

You have to execute myData.flush() after assigning all data to myData.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top