Flex - saving Serialized ArrayCollection of Objects to File - actionscript-3

Creating class with RemoteClass metateg
[RemoteClass]
public class Result
{
public function Result(){}
}
Function for writing in file
public function writeData(object:Object):void
{
var file:File = File.desktopDirectory.resolvePath("data.txt");
if (file.exists)
file.deleteFile();
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeObject(object);
fileStream.close();
}
Function for reading from file
public function readData():Object
{
var file:File = File.desktopDirectory.resolvePath("data.txt");
if(!file.exists)
return 0;
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var obj:Object = fileStream.readObject();
fileStream.close();
return obj;
}
Function when application initializing
public function initApplication():void
{
writeData(new Result()); // write object to file.
var result:Result = readData() as Result; // create Result class object and initializing it data from file
var array:ArrayCollection = new ArrayCollection(); // create collection and fill it objects of Result class
array.addItem(new Result());
array.addItem(new Result());
array.addItem(new Result());
writeData(array); // writing collection in file
var arr:ArrayCollection = readData() as ArrayCollection; // initializing new collecion of collection from file.
}
Here is the problem. Returned collection with objects of Object type.
How can I get collection of Result type objects?
Bad way below
for each (var object:Object in arr)
{
object = object as Result;
}

Use registerClassAlias() before serialization/deserialization

Related

Can not retrieve data from completely Loaded static variable AS3

Basically I was trying to read an image in binary with help of URLLoader() and convert it to a image again in flash.
I wrote 2 static methods that one of them read image and set data properties to binary and other one is the COMPLETE handler.
In COMPLETE handler I convert the binary data to ByteArray and assign it to a static variable. then tried to access to it from Main Timeline but it won't work.
The problem is my temporary variable not fully loading the static variable and when I'm checking BytesAvailable it's something like only 2.
What I'm doing wrong?
The COMPLETE handler:
public static function compHand(e:Event):void
{
var myByteArray:ByteArray = URLLoader(e.target).data as ByteArray;
LoaderToObject.ImageBytes = myByteArray;
trace(myByteArray.bytesAvailable);
if (LoaderToObject.ImageBytes.length != myByteArray.length)
{
trace(myByteArray.length);
trace(LoaderToObject.ImageBytes.length);
}
else
{
trace("successFully loaded!");
}
}
public static function loadBin(FileUrl:String)
{
var imageBytes:ByteArray=new ByteArray();
var binUrl:URLRequest = new URLRequest(FileUrl);
var binLoader:URLLoader = new URLLoader(binUrl);
binLoader.dataFormat = URLLoaderDataFormat.BINARY;
binLoader.addEventListener(Event.COMPLETE,compHand);
//trace(LoaderToObject.ImageBytes);
trace(FileUrl);
}
The function I used in main timeLine:
function loadfile(e:MouseEvent):void
{
LoaderToObject.loadBin("chick.bmp");//send file to URLLoader
imageBytes= LoaderToObject.ImageBytes;
MakeImage2(imageBytes);
}
Please help me
Because URLLoader is asynchronous, so When you try to access imageBytes, the imageBytes hasn't been load complete yet.
Your need to pass callback function to loadBin for been called when load complete
Here is an example
The load class
Class MyLoader {
private var callback:function;
private var binLoader:URLLoader;
public function load(FileUrl:String, $callback:Function):void
{
callback = $callback;
//your code
var imageBytes:ByteArray=new ByteArray();
var binUrl:URLRequest = new URLRequest(FileUrl);
binLoader = new URLLoader(binUrl);
binLoader.dataFormat = URLLoaderDataFormat.BINARY;
binLoader.addEventListener(Event.COMPLETE,compHand);
//trace(LoaderToObject.ImageBytes);
trace(FileUrl);
}
private function compHand(e:Event):void
{
var myByteArray:ByteArray = URLLoader(e.target).data as ByteArray;
if (callback != null)
{
callback(myByteArray, this);
}
}
}
LoaderToObject's load function
//make MyLoader class live, it's important
private var loadValidDic:Dictionary = new Dictionary();
public static function loadBin(FileUrl:String, callback:Function)
{
var loader:MyLoader = new MyLoader();
loadValidDic[loader] = callback;
loader.load(FileUrl, internalComplete);
}
private static function internalComplete(ba:ByteArray, loader:MyLoader):void
{
var callback:function = loadValidDic[loader];
if (callback)
{
callback(ba);
loadValidDic[loader] = null;
delete loadValidDic[loader];
}
}
Used in timeline
function loadfile(e:MouseEvent):void
{
LoaderToObject.loadBin("chick.bmp", onComplete);//send file to URLLoader
}
private function onComplete(imageBytes:ByteArray):void
{
MakeImage2(imageBytes);
}

Dynamic Object Name in AS3

I have this code in my MXML project, I want to get dynamic Stream connection for different and dynamic usernames.
private var inStream:NetStream;
private function listenStream(user:String):void
{
this["inStream"+user] = new NetStream(connection);
this["inStream"+user].play(user);
}
private function closeStream(user:String):void
{
//clear stream listener
this["inStream"+user].close();
}
But this code is not worked, How can i build dynamic object names in ActionScript3?
Thanks alot
Try Dictionary
import flash.utils.Dictionary
private var streamDict:Dictionary = new Dictionary();
private function listenStream(user:String):void
{
var key:String = getKey(user);
var lastStream:NetStream = streamDict[key] as NetStream;
if (lastStream)
{
//close the last stream or do sth else
}
else
{
streamDict[key] = new NetStream(connection);
streamDict[key].play(user);
}
}
private function closeStream(user:String):void
{
var key:String = getKey(user);
//clear stream listener
var stream:NetStream = streamDict[key] as NetStream ;
if (stream)
{
stream.close();
}
//delete the stream
streamDict[key] = null;
delete streamDict[key];
}
private function getKey(user:String):String
{
return "inStream" + user;
}

Putting Urlloader in a Class

I have searched on google en different pages i found the problem but not the solution.
I have a class i made and that class is called WebServiceController:
public class WebServiceController
{
private var url:String;
private var post:Object=new Object();
private var loader:URLLoader = new URLLoader();
private var postVariable:String="";
private var getVariable:String="";
private var Geladen:Boolean=false;
public function WebServiceController()
{
}
public function postUrlData(u:String,p:Object):String
{
url=u;
post=p;
var urlReq:URLRequest = new URLRequest (url);
var i:int=0;
for(var key:String in post)
{
//trace(key +" = " + post[key]);
if(i==0)
{
// urlVars.key = post[key];
postVariable=postVariable+""+key+"="+post[key];
}
else
{
//urlVars.key = post[key];
postVariable=postVariable+"&"+key+"="+post[key];
}
i++;
}
//trace(postVariable);
var urlVars:URLVariables = new URLVariables(postVariable);
//trace(urlVars);
// Add the variables to the URLRequest
urlReq.data = urlVars;
urlReq.method = URLRequestMethod.POST;
// Add the URLRequest data to a new Loader
//loader.load(urlReq);
// Set a listener function to run when completed
loader.addEventListener(Event.COMPLETE, onLoaderComplete);
// Set the loader format to variables and post to the PHP
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(urlReq);
function onLoaderComplete(event:Event):void
{
return loader.data;
}
}
Now from my movieclip i call the next function but it says undefined:
var wb:WebServiceController = new WebServiceController();
trace(wb.postUrlData(url,post));
I dont know how to solve this. I tried different things but it keeps saying undefined.
The URLLoader.load call is executed asynchronously so the data has not been returned when you attempt to trace it out in the class that instantiates your WebServiceController class.
To access the data in the parent class, your best bet is probably to dispatch an event from the WebServiceController class when the data has loaded and catch it in the parent class.
WebServiceController:
public function postUrlData(u:String, p:Object):void
{
url=u;
post=p;
var urlReq:URLRequest = new URLRequest (url);
var i:int=0;
for(var key:String in post)
{
//trace(key +" = " + post[key]);
if(i==0)
{
// urlVars.key = post[key];
postVariable=postVariable+""+key+"="+post[key];
}
else
{
//urlVars.key = post[key];
postVariable=postVariable+"&"+key+"="+post[key];
}
i++;
}
//trace(postVariable);
var urlVars:URLVariables = new URLVariables(postVariable);
//trace(urlVars);
// Add the variables to the URLRequest
urlReq.data = urlVars;
urlReq.method = URLRequestMethod.POST;
// Add the URLRequest data to a new Loader
//loader.load(urlReq);
// Set a listener function to run when completed
loader.addEventListener(Event.COMPLETE, onLoaderComplete);
// Set the loader format to variables and post to the PHP
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(urlReq);
}
private function onLoaderComplete(event:Event):void
{
// We can pass the event on like this
dispatchEvent(event);
}
Parent class:
public function initWebServiceController():void
{
var wb:WebServiceController = new WebServiceController();
wb.addEventListener(Event.COMPLETE, onWebServiceControllerDataLoaded);
wb.postUrlData(url, post);
}
private function onWebServiceControllerDataLoaded(event:Event):void
{
// The event target is the URLLoader instance. We can
// access the loaded data via its data property
trace(URLLoader(event.target).data);
}

How to deserialize array collection in flex

I am stuck with an issue:
In which I am serializing an array collection to a file, the array collection has two type of items an Image and a class object.
It serialize successfully, but when I de-serialize it, It simply returns array collection of objects and I found my self unable to convert these objects into Images and the class object(this class is defined by myself and having three members, two images and a shape).
I am adding the code which I used:
[Bindable] var objcnvs:ClassCanvas;
protected function btnSave_clickHandler(event:MouseEvent):void
{
//for saving data serializaion
var arrAllSave:ArrayCollection = new ArrayCollection();
for(var i:int = 0;i<arrAll.length; i++)
{
try
{
var tempCon:ConnectImage = arrAll[i];
arrAllSave.addItem({item:arrAll[i], type:"ConnectImage" });
}
catch(er:Error)
{
var tempImage:Image = arrAll[i];
var objImage:ClassImage = new ClassImage(arrAll[i]);
arrAllSave.addItem({item:objImage, type:"Image" });
}
}
// First, generate your ByteArray from the VO.
var byteArray : ByteArray = new ByteArray();
byteArray.writeObject( arrAll );
// Resolve your file location.
//var file : File = File.applicationStorageDirectory.resolvePath( "testFile.ri" );
var mapName:String = txtMapTitle.text;
var file : File = File.applicationStorageDirectory.resolvePath( 'Saved Maps/'+mapName+'.imm' );
if(file.exists == true)
{
lblWarn.text = "Map already Exists. Please enter Defferent Map Title";
}
else if(mapName == "")
{
lblWarn.text = "Please enter a title for Map.";
}
else
{
var fileStream:FileStream = new FileStream();
// Save the file to the given location.
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes( byteArray );
fileStream.close();
lblWarn.text = "Map Saved successfully";
}
}
protected function btnLoadMap_clickHandler(event:MouseEvent):void
{
// Execute the file load.
var loadFileName:String = "t1";
var request : URLRequest = new URLRequest ( "app-storage:/"+"Saved Maps/"+loadFileName+".imm" );
var receptor : URLLoader = new URLLoader( request );
// Make sure our content is interpreted as a ByteArray.
receptor.dataFormat = URLLoaderDataFormat.BINARY;
receptor.addEventListener( Event.COMPLETE, fileLoadedHandler );
}
private function fileLoadedHandler ( event : Event ) : void
{
// Retrieve the event target, cast as the URLLoader we just created
var loader : URLLoader = event.target as URLLoader;
// Retrieve the loaded data. We know it's a ByteArray, so let's cast it as well.
var data : ByteArray = loader.data as ByteArray;
// Use the ByteArray.readObject method to reconstruct the object.
var obj : Object = data.readObject();
// Cast the object and assign it to our data container.
var loadArrAll:ArrayCollection = obj as ArrayCollection;
}
The last line
var loadArrAll:ArrayCollection = obj as ArrayCollection;
this is the issue. I get the array collection but it has only a list of objects no images and no my class objects (although these are there but I found no way to convert this array collection in previous form when I serialized it.)
class that needs to be serialized/deserialised needs to implement flash.utils.IExternalizable.
public function readExternal(input:IDataInput):void - here you restore object
public function writeExternal(output:IDataOutput):void - here you saving object
best regards
Zain, you have to declare your classes like that:
package com
{
[RemoteClass]
public class myClass
{
public function myClass()
{
}
}
}

How to run function only when parameter variable has been assigned?

I need a way to wait running the parseCSV command until the readFile event has updated the content of importData. I have seen a few things about custom event dispatchers but cannot quite figure out how to use them in my situation.
private var importData : String;
public function importFile(event:MouseEvent):void {
var data:String = chooseFile();
parseCSV(importData);
}
public function chooseFile ():String {
var filetype:FileFilter = new FileFilter("CSV Files(*.csv)","*.csv");
var file:File = File.userDirectory;
file.browseForOpen("Select CSV file to import", [filetype]);
file.addEventListener(Event.SELECT, readFile);
return importData;
}
public function readFile (event:Event):void {
var filestream:FileStream = new FileStream();
filestream.open(event.target as File, FileMode.READ);
importData = filestream.readUTFBytes(filestream.bytesAvailable);
filestream.close();
}
You'll either need to add some callbacks or add some event listeners. I prefer callbacks:
function importFile(...) {
choseFile(function(file:File) {
readFile(file, parseCSV);
});
}
function choseFile(callback:Function) {
...
file.addEventListener(Event.SELECT, function(event:Event) {
callback(File(event.target));
});
}
function readFile(file:File, callback:Function) {
var data = ... read data from file ....;
callback(data);
}
What about just adding the line in the readFile function?
public function readFile (event:Event):void {
var filestream:FileStream = new FileStream();
filestream.open(event.target as File, FileMode.READ);
importData = filestream.readUTFBytes(filestream.bytesAvailable);
parseCSV(importData);
filestream.close();
}
The command will be executed as soon as importData is set.
If you wish to the custom events route, you need to dispatch your own custom Event. Each Event has a type parameter which is just a string to identify it with. For example Event.CHANGE is the same as using "change".
static public const CUSTOM = "myCustomEvent";
public function someConstructor():void {
addEventListener(CUSTOM, onCustomEvent);
}
public function testDispatch():void{
dispatchEvent(new Event(CUSTOM));
}
private function onCustomEvent(e:Event):void{
trace("custom event Dispatched");
}
So you could try something like this.
public function importFile(event:MouseEvent):void {
addEventListener(CUSTOM, onImport);
var data:String = chooseFile();
}
private function onImport(e:Event):void {
parseCSV(importData);
}
public function readFile (event:Event):void {
var filestream:FileStream = new FileStream();
filestream.open(event.target as File, FileMode.READ);
importData = filestream.readUTFBytes(filestream.bytesAvailable);
dispatchEvent(new Event(CUSTOM));
filestream.close();
}