0

I just try to save some data using flex shared object feature. The data is getting saved successfully, but when i retrieve it back, i couldn't type cast it back to my original objects. for instance, I was saving an object of my custom class, when i retrieve it back, it is of the type Object. Though I debug mode when I inspected, it has all the members of my custom class with its values. But I cant cast it from Object type back to my custom class.

Could anyone help me with this?

2 Answers 2

3

you can use registerClassAlias

registerClassAlias("com.path.to.MyClass", MyClass);
myStuff = SharedObject.getLocal("myAppStuff");
myStuff.data.whatINamedIt = myClassInstance;
myStuff.flush();

now when get it back out... you can say:

myStuff = SharedObject.getLocal("myAppStuff");
var mySavedClass:MyClass = myStuff.data.whatINamedIt as MyClass;
Sign up to request clarification or add additional context in comments.

5 Comments

Not every object will be valid after such a conversion, anything that contains links inside (child objects for Sprites/MCs, nested objects of any kind) will be invalid as a whole structure. If MyClass consists only of static data (vectors of numbers and/or strings, but nothing allocatable), it should be all right.
IMHO... if you are storing an mc inside a SO, you are doing something wrong :) EDIT: shared objects should be used for model level storage only... and bare minimum at that. Strings, ints and Booleans oh my. Anything more... and you should rethink what you are storing.
@Jason Reeves This didnt worked. When I am casting it back, it gives null. registerClassAlias("collections.ObjectMap",ObjectMap); var shapeMap:ObjectMap = so.data.shapeMap as ObjectMap; DataRepository.getInstance().shapeMap = shapeMap;
can you paste your code? registerClassAlias does work... I use it all the time for this exact thing.
Keep in mind that you need to register all classes involved, even classes that are used for member variables of MyClass.
1

You custom class must implements IExternalizable

Before getting shared object you must use registerClassAlias

        private var so:SharedObject;

        private function writeMyCustomClass():void
        {
            registerClassAlias("classes.MyCustomClass", MyCustomClass);
            so = SharedObject.getLocal("savedCustom");
            so.clear();

            var mycustom:MyCustomClass = new MyCustomClass;
            mycustom.name = "Raja";
            mycustom.age = 27;

            so.data.custom = getAndwriteCustomClassToByteArray(mycustom).readObject();
            so.flush();
        }

        private function getAndwriteCustomClassToByteArray(mycustom:MyCustomClass):ByteArray
        {
            var copier:ByteArray = new ByteArray();
            copier.position=0;
            copier.writeObject(mycustom);
            copier.position=0;

            return copier;
        }

        private function getMyCustomClass():void
        {
            registerClassAlias("classes.MyCustomClass", MyCustomClass);
            so = SharedObject.getLocal("savedCustom");
            var mycustom:MyCustomClass = so.data.custom;
        }





package
{
import flash.utils.IDataInput;
import flash.utils.IDataOutput;
import flash.utils.IExternalizable;
public class MyCustomClass implements IExternalizable
{
    public var name:String;
    public var age:int;

    public function MyCustomClass()
    {
    }

    public function writeExternal(output:IDataOutput):void {
        output.writeObject(this.name);
        output.writeInt(this.age);
    }
    public function readExternal(input:IDataInput):void {
        this.name=input.readObject();
        this.age=input.readInt();
    }
}

source : www.actionscript.org

1 Comment

i think this is not necessary. this is only needed when you want private members reconstructed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.