2 Replies Latest reply on Oct 14, 2008 4:30 AM by fargond

    Timming Problem with RemoteObject

    Sagar S De
      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());
      }
      }
      }