actionscript-3apache-flexflex4flex4.7

Flex 4 registerClassAlias so I can load a SharedObject next time I start the App


I have an application that is loading an swf. I can use that swf several times until there is a new one on the server. Therefore, I want to save as a SharedObject the loader containing the swf. Next time I could use the loader from the SharedObject instead of having to load the swf again (since it was already loaded one time).

For that I do the following:

var sObject: SharedObject = SharedObject.getLocal('PrefsObj');//my SharedObject.
registerClassAlias( "remoteSharedObject.NxLoader", NxLoader );//I register every time my custom Loader class which just extends from Loader.

if(sObject && sObject.data &&  sObject.data.loader)//I check if the sharedObject is empty? If not I get the previously saved loader.
  loader = sObject.data.loader as NxLoader;
else//if it's empty I create the loader and load the bytes
{
  loader = new NxLoader();
  loader.loadBytes(swfBA,context);
}     
this.addChild(loader);//add the loader.
sObject.data.loader = loader;//save the loader on my sharedObject for next time.
sObject.flush();//done.

What am I doing wrong? The loader inside the SharedObject is not recognized and comes as an Object. When it reaches the addChild method - Boom! -> Main Thread (Suspended: TypeError: Error #2007: Parameter child must be non-null.)

Thanks


Solution

  • I see; so you want to cache your SWFs on the local system to reduce server overhead. According to Adobe's Reference...

    "The SharedObject class is used to read and store limited amounts of data on a user's computer ...

    ... sometimes SWF files may not be allowed to write local shared objects, and sometimes the data stored in local shared objects can be deleted without your knowledge."

    If you look at the user default settings for SharedObject storage, you notice a 100KB maximum on data stored there. Saving an entire SWF is therefore impractical (if not impossible) as most files are greater than this maximum. This makes sense as to why you'd receive a null value from your saved object.