简体   繁体   中英

SharedObject and Dictionary problems in Flash Builder

I'm usin Flash Builder to create some actionscript code that uses SharedObjects. First question: how can I delete my local SharedObject in Flash Builder? I am debugging my program and the SharedObject sems to persist between runs. I want to start fresh and clean with no SharedObject storing my data. How do I get rid of it?

Also, in my SharedObject, I used mySharedObject.data["mykey"] to store a Dictionary. This Dictionary will have String keys and values of MyCustomClass. The problem is that when I later try to loop over the values of this Dictionary, I get error #1034 cannot convert object to type MyCustomClass. It seems like I can put an item of type MyCustomClass into this dictionary, but I can't get the item back out as anything other than an object.

Any idea what is going wrong?

Those are essentially two questions, so should have been asked as two questions. Anyway, I'd answer them here but still prefer that you break them up in two parts (possibly leave a link to the other one here for reference sake):

Local shared object, are useful exactly for persistence across runs. And then there's SharedObject.clear() to clear the state as required.

For you second issue, Shared Object's serialize your object into AMF , so that it can be written to disk or sent over network using RTMP . Now, your custom class can't really be serialized in AMF . What actually happens is that the public properties (and dynamic ones, if the class is declared dynamic) are serialized into the structure. So, the public data is stored... but it's essentially a general Object . To work around that, you can have a

public static readFrom(object:Object):MyCustomClass
type function, which would read the properties from the passed object to construct a
 new MyCustomClass  
representing that information.

There are ways to register your class with the player to be stored in SharedObject (see here )... but you need to make sure that the code that de-serializes that data is aware of the class as well.

To make a class available for conversion, in your global initialization use registerClassAlias() call with MyCustomClass and its fully qualified name as parameters. The manual. Say your custom class is foo.bar.TheClass , you write:

registerClassAlias('foo.bar.TheClass',foo.bar.TheClass);

In order to drop old SO use delete call against so.data["mykey"] and do so.flush() . Edit: SharedObject.clear() is way better.

1/ Being persistent is one of the particularity of a SharedObject. To cleanup all its content, you need to call the clear method.

var shareObject:SharedObject = SharedObject.getLocal('justatest'); 
shareObject.data.test = 'test';
trace(shareObject.data.test)
shareObject.clear();
trace(shareObject.data.test)

output

test
undefined

2/ To store complex data types in SO, you need to use flash.net.registerClassAlias ( example here )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM