Objects saved in a shared object are not keeping their data type after restart. Any ideas?

柔情痞子 提交于 2019-12-08 07:49:05

问题


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?


回答1:


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.




回答2:


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

Manual




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/14846062/objects-saved-in-a-shared-object-are-not-keeping-their-data-type-after-restart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!