1

I am building an Adobe AIR app and I'm storing a Dictionary in my SharedObject. Something along the lines of what is mentioned here.

But why is the length of my dictionary always null. I end up having to manually keep track of the length which seems to work but doesn't fill me with confidence.

Also, can someone explain when exactly readExternal and writeExternal get called? I find that they are called multiple times (for what seems to me unnecessary). As it's a dictionary, adding the same value multiple times isn't proving a problem but again, wondering if I have setup something wrong here to trigger this.

This is what I have a

public class SharedObjectDictionary implements IExternalizable {

private var _dictionary:Dictionary;
private var _length:int;

public function addItem and removeItem
{
... just adds/removes to my _dictionary ...
... and keeps track of the length ...
}

public function readExternal(input:IDataInput):void
{
    var count:int = input.readInt();
    for (var i:Number = 0;i<count;i++)
    {
        var key:String = input.readObject();
        var val:String = input.readObject();

        addItem(key,val);
    }
}

public function writeExternal(output:IDataOutput):void
{
    output.writeInt(_length);

    for (var key:Object in dictionary)
    {
        output.writeObject(key);
        output.writeObject(dictionary[key]);
    }
}

}

When I make the call to store this SharedObject as such

    var so:SharedObject = SharedObject.getLocal("xxxx");
    so.data["xxxxx"] = instance of the SharedObjectDictionary above;
    so.flush();
    so.close();
    so = null;

I find that readExternal gets called once and writeExternal gets called 3 times.

Thanks!

Community
  • 1
  • 1
m.y
  • 691
  • 9
  • 21

1 Answers1

0

The length of your Dictionary is always null because the Dictionary doesn't define such a property. If you tried to access a property that doesn't exist on some other class, you would get a compile time error. But since the Dictionary class is dynamic, no such compile error will occur. The only solution is to keep track of the length yourself.

I can't comment on your other question about readExternal/writeExternal ... do you have any code you can show that demonstrates this?

Sunil D.
  • 17,983
  • 6
  • 53
  • 65
  • ah makes sense for the null then! See my edited question with more code demonstrating what I do. – m.y Jul 12 '13 at 17:13
  • 1
    Just a tip: I really like the 'LinkedMap' found in the as3commons-collection package [as3commons.org](http://www.as3commons.org) . It's basically a combination of a dictionary and an array, so 'length' - along with many other useful stuff - is already taken care of. – T. Richter Jul 16 '13 at 10:31