0 Replies Latest reply on Jul 30, 2009 12:57 AM by vpxav

    How to optimize this code?  (points will be awarded)

    vpxav

      Hello everyone, I'm a beginner in flex and I managed to develop my 1st application.

      The application is running really fast on a computer but not from a portable device (in my case a nokia N810), it's extremely slow (many seconds before I can see the data).


      I would be very happy if someone could modify the following portion of code to optimize it and give me explanations about the modifications made.

      The portion of code that is very slow is the following:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" width="100%" height="100%" creationComplete="initializeHandler(event);">
          <mx:Script>
              <![CDATA[
              private function initializeHandler(event:Event):void{
                      listData1.send();listData2.send();
                  }
                 import mx.controls.Alert;
                 import flash.net.URLLoader;
                 import flash.net.URLVariables;
                 import flash.net.URLRequest;
                 import flash.net.URLRequestMethod;
                 import flash.events.Event;
                 // function that sends the id and target value of the element to update when slider changed
                     private function onSliderChanged(e:Event):void{
                         var eltID : String = elementID[e.target.repeaterIndices].text;
                         var sliderValue: String = String(hSlider[e.target.repeaterIndices].value);
                        
                             var chargeur:URLLoader = new URLLoader ();
                             
                             var _vars : URLVariables = new URLVariables();
                                 _vars.element_id = parseInt(eltID);
                                 _vars.target = parseInt(sliderValue)*2.5;
                           
                             var adresse:URLRequest = new URLRequest("scripts/update_element.php");
                             adresse.method = URLRequestMethod.POST;
                             adresse.data = _vars;
                         
                             chargeur.load(adresse);
                             chargeur.addEventListener(Event.COMPLETE, finDuChargement);                 
                      }
                 
                  //======================================================================================= ==============
                  // function that sends the id and target value of the element to update when buttonBar is clicked
                     import mx.events.ItemClickEvent;
                  private function clickHandler(event:ItemClickEvent):void {
                      var eltID : String = elementID[event.target.repeaterIndices].text;
                      var eltVal: String = elementPrefVal1[event.target.repeaterIndices].text;
                      switch (event.index) {
                          case 0:
                              eltVal = '0';
                              break;
                          case 1:
                              // eltVal = element's prefval
                              break;
                          case 2:
                              eltVal = '250';
                              break;
                          default:
                              break;
                     }
                     var chargeur:URLLoader = new URLLoader ();
                             
                     var _vars : URLVariables = new URLVariables();
                         _vars.element_id = parseInt(eltID);
                         _vars.target = parseInt(eltVal);
                   
                     var adresse:URLRequest = new URLRequest("scripts/update_element.php");
                     adresse.method = URLRequestMethod.POST;
                     adresse.data = _vars;
                 
                     chargeur.load(adresse);
                     chargeur.addEventListener(Event.COMPLETE, finDuChargement);
                  }
                 
                  private function clickHandler2(event:ItemClickEvent):void {
                      var eltID : String = elementID2[event.target.repeaterIndices].text;
                      var eltVal: String = elementPrefVal2[event.target.repeaterIndices].text;
                      switch (event.index) {
                          case 0:
                              eltVal = '0';
                              break;
                          case 1:
                              // eltVal = element's prefval
                              break;
                          case 2:
                              eltVal = '250';
                              break;
                          default:
                              break;
                     }
                     var chargeur:URLLoader = new URLLoader ();
                             
                     var _vars : URLVariables = new URLVariables();
                         _vars.element_id = parseInt(eltID);
                         _vars.target = parseInt(eltVal);
                   
                     var adresse:URLRequest = new URLRequest("scripts/update_element.php");
                     adresse.method = URLRequestMethod.POST;
                     adresse.data = _vars;
                 
                     chargeur.load(adresse);
                     chargeur.addEventListener(Event.COMPLETE, finDuChargement);
                  }
                  //======================================================================================= ==============
                  private function finDuChargement(event:Event):void {
                      var contenu : XML = XML(event.target.data);
                     
                     if(contenu=="true") {
                      }
                      else {
                          Alert.show('Insert KO');
                      }
                  }
              ]]>   
          </mx:Script>   
          <mx:HTTPService id="listData1" url="scripts/listelements1.php" resultFormat="e4x" useProxy="false" method="POST">
              <mx:request xmlns="">
                  <zone_id>{parentApplication.currentZoneIDButton.text}</zone_id>
              </mx:request>
          </mx:HTTPService>
          <mx:HTTPService id="listData2" url="scripts/listelements2.php" resultFormat="e4x" useProxy="false" method="POST">
              <mx:request xmlns="">
                  <zone_id>{parentApplication.currentZoneIDButton.text}</zone_id>
              </mx:request>
          </mx:HTTPService>
          <mx:Panel id="elementsMgt"
              title="Elements Management"
              titleIcon="@Embed(source='images/element_on.png')"
              width="100%" height="100%">               
              <mx:Canvas width="100%" height="100%" cornerRadius="4" >
                  <mx:Tile left="10" right="10" top="10" bottom="10" cornerRadius="4">
                      <mx:Repeater id="repeater1" dataProvider="{XML(listData1.lastResult).element}">
                          <mx:Panel title="{String(XML(repeater1.currentItem).@name)}" toolTip="{String(XML(repeater1.currentItem).@description)}" width="210">                       
                              <mx:ButtonBar id="buttons" width="100%"  itemClick="clickHandler(event);">           
                                  <mx:dataProvider>                             
                                        <mx:Object icon="@Embed(source='images/element_off.png')"/>
                                      <mx:Object label="DEFAULT"/>
                                      <mx:Object icon="@Embed(source='images/element_on.png')"/>
                                  </mx:dataProvider>
                              </mx:ButtonBar>
                              <mx:HSlider id="hSlider"
                                  width="100%"
                                  minimum="0"
                                  maximum="100"
                                  value="{(XML(repeater1.currentItem).@actval)/2.5}"
                                  dataTipPlacement="bottom"
                                  labels="['0%','50%','100%']"
                                  snapInterval="1"
                                  tickInterval="25"
                                  liveDragging="false"
                                  allowTrackClick="true"
                                  change="onSliderChanged(event)"
                              />
                              <mx:Text id="elementPrefVal1" text="{String(XML(repeater1.currentItem).@prefval)}" visible="false" includeInLayout="false" height="0" width="0"/>
                              <mx:Text id="elementID" text="{String(XML(repeater1.currentItem).@id)}" visible="false" includeInLayout="false" height="0" width="0"/>
                          </mx:Panel>
                      </mx:Repeater>
                      <mx:Repeater id="repeater2" dataProvider="{XML(listData2.lastResult).element}">
                          <mx:Panel title="{String(XML(repeater2.currentItem).@name)}" toolTip="{String(XML(repeater2.currentItem).@description)}" width="210">
                              <mx:ToggleButtonBar width="100%" height="64" selectedIndex="{(XML(repeater2.currentItem).@actval)/125}" itemClick="clickHandler2(event);"  bottom="3">           
                                  <mx:dataProvider>                             
                                        <mx:Object icon="@Embed(source='images/element_off.png')"/>
                                      <mx:Object label="DEFAULT"/>
                                      <mx:Object icon="@Embed(source='images/element_on.png')"/>
                                  </mx:dataProvider>
                              </mx:ToggleButtonBar>
                              <mx:Text id="elementPrefVal2" text="{String(XML(repeater2.currentItem).@prefval)}" visible="false" includeInLayout="false" height="0" width="0"/>
                              <mx:Text id="elementID2" text="{String(XML(repeater2.currentItem).@id)}" visible="false" includeInLayout="false" height="0" width="0"/>
                          </mx:Panel>
                      </mx:Repeater>               
                  </mx:Tile>
              </mx:Canvas>
          </mx:Panel>
      </mx:Module>

       

      Thank you for your help and the time spent on helping beginners...

       

      I won't forget to give points for the answers that best helped and solved my problem.