Highlighted

Timming Problem with RemoteObject

Sagar_S_De

03-09-2008

Hi,

I am Sagar S. De, I have faced a problem with Flex
DataService described below…

I am using flex RemoteObject to get data from Database and
then I am trying to set the data in ArrayCollection. But the data
is not assigned to ArrayCollection. When I am debugging I have
found that the ArrayCollection is assigned before data came from
Database. How do I solve the problem?

Here is a code snippet where I have faced the problem exactly
(In alert box Result2 comes before Result1).



package visualsqlbuilder.logic {

import mx.collections.ArrayCollection;

import mx.controls.Alert;

import mx.rpc.events.FaultEvent;

import mx.rpc.events.ResultEvent;

import mx.rpc.remoting.mxml.RemoteObject;



public class Table {

public var tableNames : ArrayCollection = new
ArrayCollection();

/* RETURNS ALL TABLE NAMES FORM THE DATABASE OF THE
SPECIFIED SCHEMA */

public function getTables(connectionTypetring,
schemaNametring) : ArrayCollection {

var remoteGetTables : RemoteObject = new RemoteObject();

remoteGetTables.destination = "DBDetails";

remoteGetTables.addEventListener(ResultEvent.RESULT,
resultGetTables);

remoteGetTables.addEventListener(FaultEvent.FAULT,
faultGetTables);

remoteGetTables.showBusyCursor = true;

remoteGetTables.getDBTables(connectionType, schemaName);



//Executes Before data came from Database and show tableNames
as blank ie. null

Alert.show("Result 2: " + tableNames.toString());



return tableNames;

}



public function resultGetTables(event:ResultEvent) : void {

var result : ArrayCollection = event.result as
ArrayCollection;

tableNames.removeAll();

for(var i : int = 0; i<result.length; i++) {

var string = result.getItemAt.toString();

tableNames.addItem;

}

//Executes later; ie. After getting data from Databse and
show tableNames correctly

Alert.show("Result 1: " + tableNames.toString());

}



public function faultGetTables(event:FaultEvent) : void {

mx.controls.Alert.show(event.fault.toString());

}

}

}

Replies

Highlighted

riesvantwisk

11-09-2008

You need to realize that Flex does most of the asynchronous,
that means that the function
remoteGetTables.getDBTables(connectionType, schemaName); will
returns immediately.



So if you want to place an Alert somewhere, do it in your
response function where you currently show your Result 1.



It's currently hard to understand what you really want to do
with your dataset. But further processing of real data needs to be
done in the function resultGetTables or other functions executed
when the result comes back.



Ries

.