Saving application state in AS3 AIR Mobile

The class below can be used for storing and retrieving object from a SharedObject.
Fictive usage example code:

// Creating object
var objectToStore:YourClass = new YourClass();
objectToStore.property = "value";

// Store it
Store.Set("key", objectToStore);

// And retrieving it again.
var objectFromStore:YourClass = Store.Get("key") as YourClass;

// Get proof...
trace(objectFromStore.property); // Should output "trace"

The class used for storing and retrieving your objects dynamically

package Memory 
{
    import flash.net.registerClassAlias;
    import flash.net.SharedObject;
    import flash.utils.getDefinitionByName;
    import flash.utils.getQualifiedClassName;

    public class Store 
    {
        private static var sharedObject:SharedObject = SharedObject.getLocal("MemoryGame");
        
        public static function Set(key:String, value:Object):void
        {
            var classAlias:String = getQualifiedClassName(value);
            var classObject:Class = Class(getDefinitionByName(getQualifiedClassName(value)));
            registerClassAlias(classAlias, classObject); 
            
            sharedObject.data[key] = value;
            sharedObject.flush();
        }
        
        public static function Get(key:String):*
        {
            var value:Object = sharedObject.data[key];
            var classAlias:String = getQualifiedClassName(value);
            var classObject:Class = Class(getDefinitionByName(getQualifiedClassName(value)));
            registerClassAlias(classAlias, classObject); 
            
            return value as classObject;
        }
        
        public static function Remove(key:String):void
        {
            delete sharedObject.data[key];
            sharedObject.flush();
        }
        
        public static function Clear():void
        {
            sharedObject.clear();
        }
    }
}
comments powered by Disqus
mycreativity.nl

mycreativity.nl - all about my creations | copyright © 2009-2017 mycreativity