13 Replies Latest reply on Feb 9, 2007 9:58 AM by arkcto

    Asychronous calls with Remote objects

    arkcto
      Hi,

      Is there anyway I can block execution until I get the asychronous call to a remote object ? Basically I don't want to do anything until the call is completed.

      My code looks like this :

      Approach 1
      --------------

      // function to set tha data after you get the result.
      private function setData(event:ResultEvent):void {
      userData = new ArrayCollection(event.result as Array); // set the data after you get it.
      }

      // make the remote object call
      sampleRO.getRemoteData(username);

      // Add event listener to handle the result -- see above for code for setData
      sampleRO.addEventListener("result",setData);

      // Now I want to display a data grid based on use but ONLY when userData is retrieved i.e. when userData != null
      // so I put the following code to basically block the execution until userData is populated by the remote call.
      while (userData ==null) {
      ; // Do nothing
      }
      // display user data here in a data grid - code not shown

      //but my "blocking" while loop just makes the app hangs for 30 secs and timeouts (even though the actual call takes only 2 secs).


      I thought that maybe I need to use Aysnchronous Token pattern here, so I tried a second approach by going through some examples of Asychronous Token pattern using webservices and translating the exampel to Remote Object. (unsuccesfully). Can someone please post an equivalent version for RemoteObjects (not web services). Below is my version (that doesn't work) using Token pattern

      Approach 2
      ----------------

      sampleRO.showBusyCursor = true;
      sampleRO.addEventListener("result",setDataWithToken);
      sampleRO.myToken = "syncMe";

      private function setDataWithToken(event:ResultEvent):void {
      userData = new ArrayCollection(event.result as Array); // set the data after you get it.
      var myT:Object = result.token;
      while (myT != "syncMe") {
      // do nothing
      }
      }
      The compiler complains saying that I can't run operations on AsyncToken or something to that effect.


      I believe approach 2 is closer to the correct approach, but I just need to know how to implement Asynchronous Token using Remoteobject.

      Your help much appreciated.

      Thanks.










        • 1. Re: Asychronous calls with Remote objects
          michael_ramirez44 Level 1
          Are you trying to prevent calling the same RemoteObject twice before the results of the first returns?
          • 2. Re: Asychronous calls with Remote objects
            arkcto Level 1
            >>Are you trying to prevent calling the same RemoteObject twice before the results of the first returns?

            No. Why are you saying that ? All I want to do is to suspend execution of the remaining code until the RemoteObject call is completed. I am trying to mimic synchronous behavior with an asynchronous call basically.

            • 3. Re: Asychronous calls with Remote objects
              michael_ramirez44 Level 1
              quote:

              Originally posted by: peterent
              Flex is deliberately designed NOT to work synchronously. It causes too many problems and the Flash Player has to continue running to process events anyway.

              Your best bet is to go with the flow - when the Alert comes up it is modal and input will not be allowed outside of the Alert. Your program should be written in such a way that it will not continue processing until the Alert's event listener is called.
              • 4. Asychronous calls with Remote objects
                arkcto Level 1
                I gather that about FLash and Asynchronous communications.

                My question is - can someone give me an example of Asyynchronous Token pattern for Remote Object or point out why my while loop coding doesn't work ?

                Thanks.
                • 5. Asychronous calls with Remote objects
                  parallaxed
                  Hi arkcto,

                  I can't see any reason why you'd want to stop execution while waiting for your result.

                  If you need to stop the user inputting anything while the call is waiting for a result, you can use a Busy Cursor.
                  • 6. Re: Asychronous calls with Remote objects
                    parallaxed Level 1
                    By "inputting anything", I refer to interation of any sort...
                    • 7. Re: Asychronous calls with Remote objects
                      arkcto Level 1
                      Hi parallalexed,

                      I think I see where you are getting at. Basically I don't want to display the datagrid until the dataprovider of the grid is filled with data. And that happens only when the remote object call is completed.

                      busy cursor doesn't help. Yes it's displaying the busy cursor, but its still showing an empty grid with the busy cursor. Is there a way I can tell the grid to refresh itself once the data is available ?

                      Thanks.
                      • 8. Re: Asychronous calls with Remote objects
                        michael_ramirez44 Level 1
                        Set your DataGrid visible=false. When you get your results set visible=true; If you are using data binding the grid should refresh once the data is available.
                        • 9. Asychronous calls with Remote objects
                          arkcto Level 1
                          >>When you get your results set visible=true; If you are using data binding the grid should refresh once the data is available.
                          And how do I find out if I got my results ?

                          I tried doing
                          datagrid.visible = false;
                          while (userData == null) {
                          // do nothing
                          }
                          datagrid.visible = true;

                          This code timed out (even though the actual RemoteObject call (found out by trace statements) only takes 2 secs).

                          Also, regarding making the variable bindable - is the following code sufficient ?

                          [Bindable]
                          public var userData:ArrayCollection;
                          ....
                          ....
                          var userGrid:DataGrid = new DataGrid();
                          userGrid.dataProvider = userData;


                          Thanks.
                          • 10. Re: Asychronous calls with Remote objects
                            parallaxed Level 1
                            Hi Arkcto,

                            The solution being imposed by your while loop follows isn't the best way to go. Flex would expect you to do things differently (don't think in terms of a thread model). Ignoring the idea of looping around to stop execution, there are other ways to accomplish this (that are less complicated).

                            Flex is heavy on the Patterns - with data binding readily available you can do this using an MVC pattern lickedy-split. Stay Asynchronous.

                            Set your data grid to be invisible initially. Using your sampleRO:

                            sampleRO.showBusyCursor = true;
                            sampleRO.addEventListener("result",setDataWithToken);

                            [Bindable]
                            public var userData:ArrayCollection = new ArrayCollection();

                            // set your
                            <mx:DataGrid id="userGrid" dataProvider="{userData}" />

                            // Called when the result comes back
                            setDataWithToken(event:ResultEvent) {
                            // reset your dataProvider
                            userGrid.dataProvider = event.result as ArrayCollection;
                            // set visible to true
                            userGrid.visible = true;
                            }

                            Flex is an event-driven framework, and apps should be written in the same vein.

                            while (someOperationIncomplete)
                            {
                            // do nothing
                            }

                            shouldn't be anywhere near a Flex app =)
                            • 11. Re: Asychronous calls with Remote objects
                              arkcto Level 1
                              Doesn't work. I already tried it.

                              Thanks anyway.
                              • 12. Re: Asychronous calls with Remote objects
                                Cheree Singal Level 1
                                Hi Arkcto,

                                I think a simple way wud be to give "dataGrid.visible=false" initially when u dont have anything to display in it & then give "dataGrid.visible=true" in setData() itself coz that is the function which will be called ONLY when remote object returns the result. This works for me, it shud for u too:-)

                                Cheree
                                • 13. Re: Asychronous calls with Remote objects
                                  arkcto Level 1
                                  You the man Cheree.

                                  That's what it was ..
                                  I moved all my datagrid code to the SetData and it works just fine.

                                  A simple one in the end.

                                  Thank you.