11 Replies Latest reply on Apr 1, 2009 7:05 AM by Peter Hahmann

    Remote Object: Do I have to call "read" every time I change data?

    EvolvedDSM Level 2
      I have a dataset of about 500 records that I need to read into a datagrid and be able to add, modify or delete. Using remoteObject, I call a 'read' method which gets the data and populates the datagrid. Reading the data alone takes about 6-8 seconds, which is acceptable when first opening the application. However, to keep the data fresh after each addition or modification, I have to re-read the data into an arrayCollection.

      Is there any way to update only the record(s) that is affected by the changes, so I don't have to keep reading the whole table and refreshing the arrayCollection each time I make a change?
        • 1. Re: Remote Object: Do I have to call "read" every time I change data?
          Peter Hahmann Level 1
          EvolvedDSM,

          There are 2 things I can think of.

          1 - You want to change the data of your 3rd record, id_record = 3, for instance. After updating your DB, you just select this record again and updates the item of your ArrayCollection.

          2 - You select again the 3rd record, and before changing the data you save this object to a temporary object. So if there is any problem updating the record at your DB you just update you ArrayCollection object with the temporary ("old") object.

          I hope that I could express myself clearly.

          Regards,
          Peter
          • 2. Re: Remote Object: Do I have to call "read" every time I change data?
            EvolvedDSM Level 2
            So you are saying that I should just make changes within the arrayCollection, and once done with the application, do a sweeping update of all records? Sorry if this isn't what you meant, just how I interpretted it. I could make changes to the arrayCollection only, which wouldn't require the lengthy call times of RemoteObject, however I'm not sure how to update all records of an arrayCollection back to a database. Any help with that? thanks
            • 3. Re: Remote Object: Do I have to call "read" every time I change data?
              Peter Hahmann Level 1
              Yes, this would be one option. When receiving the data from your DB, you could save this data to your primary (display) ArrayCollection and a copy (source.concat()) to a secondary ArrayCollection. After modifying the items on your primary ArrayCollection you could compare both Collections and generate a third Collection containing only the changed objects. This third collection would be the one you send back to be updated at your DB.

              The second method, which i would particularly recommend is saving each modification and just returning the modified object. For instance, your Collection has 10 items and you are modifying the 3rd item, id = 3. After editing this item you could save its index (2) and your update method returns only the modified item instead of a new list (select * from my_table where id = 3). Now knowing its index (as var or AsynkToken property) you could easily update/substitute this item on your collection.

              Choosing between each option will depend only on what you intend do do. You may also create some variations in order to get more or less security while changing your data.

              Regards,
              Peter
              • 4. Re: Remote Object: Do I have to call "read" every time I change data?
                EvolvedDSM Level 2
                Well Peter, I like your 2nd option. When I call for the updated record in my update method, how can I get that to replace the outdated record in the arrayCollection so that the data being viewed is current?

                UPDATE query on record 3
                SELECT query on record 3
                <need help to replace record in arrayCollection with result of SELECT query>
                View updated record in application
                • 5. Re: Remote Object: Do I have to call &quot;read&quot; every time I change data?
                  Peter Hahmann Level 1
                  There are two ways I can thing of:

                  1 - You can use the setItemAt(obj, index) property.
                  2 - You can removeItemAt(index) and then addItemAt(obj, index).

                  I don't see why using the 2nd option, but it's possible.


                  Regrads,
                  Peter
                  • 6. Remote Object: Do I have to call &quot;read&quot; every time I change data?
                    EvolvedDSM Level 2
                    Peter you are the man. I am actually using a combination of 1 & 2 just because sometimes I'm not making changes to a record, but maybe adding or removing one!

                    The code was soooo simple.

                    addItem(obj) is all that's required, no index since it will just add at the end of the array (and the position in the array isn't important, it's my data on the server that is).

                    removeItemAt(datagrid.selectedIndex) removes the selected item. Works great.

                    setItemAt(obj, datagrid.selectedIndex) again works fantastic.

                    Thanks a million Peter
                    • 7. Re: Remote Object: Do I have to call &quot;read&quot; every time I change data?
                      Peter Hahmann Level 1
                      You welcome.

                      You are using datagrid.selectedIndex as you said. Well it works, but as your code is being processed and specialy the RO that is asynchronous to flex/as, I would recommend you save the object at the moment you select it and use the object instead the grids selected index. Example:

                      private var myObj:Object

                      private function grid_changeHandler(e:ListEvent):void{
                      myObj = DataGrid(e.target).selectedItem;
                      }

                      private function removeItem():void{
                      var index:int = myArrayCollection.getItemIndex(myObj);
                      myArrayCollection.removeItemAt(index);
                      }

                      It is more code but you will guarantee less errors and in some cases a better design pattern( http://en.wikipedia.org/wiki/Design_pattern_(computer_science)).

                      Regards,
                      Peter
                      • 8. Re: Remote Object: Do I have to call &quot;read&quot; every time I change data?
                        EvolvedDSM Level 2
                        Oh, I see what you mean. When using getItemIndex(object), does it find the item's index # by matching the Object's values with that of the item in the array, or does the object take on a property value of the array index.

                        Like, when I do var obj = datagrid.selectedItem, is obj taking on the array index as an additional property? Or does it just match the properties with an item that's in the array?
                        • 9. Re: Remote Object: Do I have to call &quot;read&quot; every time I change data?
                          Peter Hahmann Level 1
                          Every instance has a property called UID, (when debugging your app: Object(@ff542s), sou the UID is ff542s. This UID is the allocated memory in your app for this instance. Sou when you say that your var is equal to another var this is what happens.

                          var A = B -> A is now a reference to B, so both will have the same UID. A is just a pointer.

                          So when you're using getItemIndex, flex will look for an object with the same reference, teh same UID.

                          Now if you say that:

                          var A = new A();
                          A.name = B.name;
                          A.id = B.id

                          Although all properties have the same value, your variables are not equal, as their reference/UID is different. Var A is not a pointer to B, var A is just a variable with the same properties.

                          Regards,
                          peter

                          ps. take my e-mail so you can contact me direcly: phahmann@gmail.com (at the moment i'm not able to access it from my office, pretty stupid, i know!)
                          • 10. Re: Remote Object: Do I have to call &quot;read&quot; every time I change data?
                            EvolvedDSM Level 2
                            Oh ok that makes sense. I really need to start digging into what goes on behind Flex apps so I can take advantage of things like this. Most of our apps I've done have used a data service via FDS/LCDS, so there was never much thought involved because those services basically automate everything. Thanks again Peter, I've learned much here in this thread. I'll hold on to this email just incase something else related arises -- I'm always looking for better ways to handle processes.