3 Replies Latest reply on Jan 26, 2010 8:07 AM by Matt Le Fevre

    Why 'HTTPService result' is not refreshing itself ?

    Rymasek

      I have application which, should connect to database in set time intervals, and pull specific data.

      That data in database is changing constantly, and I want visualize them in datagrid.

       

      so look at the code, and problem description below it :

       

      // ---------------------------------------------

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                      layout="absolute"
                      creationComplete="init()">
          <mx:Script>
              <![CDATA[
                  import mx.rpc.events.ResultEvent;
                  import mx.collections.ArrayCollection;
                  import flash.utils.Timer

       

                  [Bindable]
                  private var currentPosition_AC:ArrayCollection=new ArrayCollection();

       

                  private function init():void
                  {
                      trace("timer started")
                      var timer:Timer=new Timer(5000, 1);
                      timer.addEventListener(TimerEvent.TIMER_COMPLETE, pullData)
                      timer.start()
                  }

       

                  private function pullData(e:TimerEvent):void
                  {
                      trace(e + " data pull")
                      read_data();
                      init();
                  }

       

                  private function read_data():void
                  {
                      readRequest.send();
                      trace("read ok!")

                  }

       

                  private function setup_AC(event:ResultEvent):void
                  {
                      currentPosition_AC.removeAll()               
                      trace("after remove" + currentPosition_AC.length)       
                      currentPosition_AC = event.result.message.pos               
                      trace("after add "+currentPosition_AC.length)               
                  }
              ]]>
          </mx:Script>

       


          <mx:HTTPService id="readRequest"
                          url="http://www.mysite.com/monitor/read_record.php"
                          result="setup_AC(event)"> 
          </mx:HTTPService>

       

          <mx:Canvas x="67"
                     y="235"
                     width="22"
                     height="22"
                     backgroundColor="#D89800"
                     id="myObject">
          </mx:Canvas>

       

          <mx:DataGrid x="24"
                       y="23"
                       width="668"
                       dataProvider="{currentPosition_AC}">

       

              <mx:columns>
                  <mx:DataGridColumn headerText="Column 1"
                                     dataField="idmessage"/>
                  <mx:DataGridColumn headerText="Column 2"
                                     dataField="objectx"/>
                  <mx:DataGridColumn headerText="Column 3"
                                     dataField="objecty"/>
              </mx:columns>
          </mx:DataGrid>

       

      </mx:Application>

       

      // ---------------------------------------------

       

      and problem is because data is not chaging in datagrid, event though this piece of code :

       

        currentPosition_AC.removeAll()               
         trace("after remove" + currentPosition_AC.length)       
         currentPosition_AC = event.result.message.pos               
         trace("after add "+currentPosition_AC.length)          

       

      is working properly, I mean, it shows that arrayCollection is empty (first trace), array collection has objects (second trace)

       

      I've tested this with debbuger and breakpoints, and it shows that data is always the same, although it has changed on the server.

      I'm positive about that because if I look at URL: http://www.mysite.com/monitor/read_record.php, it clearly shows me that data

      is changing constantly...

       

      ... so why then, array collection : currentPosition_AC is taking always the same values... to be specific... it takes correct

      values for the first time when application is run, and then, result is keeping first data, though it is changing !

      Why is that happening ?? Any idea ?

       

      Thanks

        • 1. Re: Why 'HTTPService result' is not refreshing itself ?
          Matt Le Fevre Level 4

          I've had the same kind of issues of data not correctly refreshing, and put it down to a 'feature' of flex. I found that if you always POST data with the HTTPService, it will always return the most up to date results, if not then it behaved erratically, sometimes doing so and other times as you've mentioned, only retrieving the information once and never again.

           

          No need to alter the PHP file, it'll simply ignore the POST'ed data, so try the following change in your Flex code;

           

          <mx:Script>
          <![CDATA[
                    [Bindable]
                    public var DumpData:String = "sdfhsjkldhfjksdhfjksh";
          ]]>
          </mx:Script>
          
          
          <mx:HTTPService id="readRequest" method="POST" useProxy="false" url="http://www.mysite.com/monitor/read_record.php" result="setup_AC(event)">  
               <mx:request xmlns="">    
                      <DataDump>
                          {DumpData}
                      </DataDump>  
               </mx:request>  
          </mx:HTTPService>
          
          1 person found this helpful
          • 2. Re: Why 'HTTPService result' is not refreshing itself ?
            oldMster Level 3

            Another thing to try is to assign an id to the datagrid (assume id="myDG" for this example) then at the end of you setup_AC function, add:

             

            myDG.invalidateList();

             

            Which will force the datagrid to update its data from the datasource.

             

            Mark

            • 3. Re: Why 'HTTPService result' is not refreshing itself ?
              oldMster Level 3

              Grizzzzz probably has the answer - if you are making exactly the same http call, the browser flex is hosted in may not be making the call at all, but satisfying the call from local cache.  Since Flex uses the hosting browser to make the http call, there isn't anything flex can do to influence it's behavior.

               

              To overcome this I usually add another name/value pair to the call that is always different, so the browser won't try to use cache.  In you read_data function, have another 'above the script' variable that is incremented with each call, and send that as a name value pair of 'dontcache=value'.  as long as it is different, then the cache won't be used.

               

              Mark