6 Replies Latest reply on Apr 23, 2010 7:13 PM by David_F57

    How to find the Value of a Property in the Last Object of An Array Collection

    jimmyoneshot Level 1

      I want to allow my users to add their own values to my array collection 'linksFullAC' using the following code which gets fired when they click a button:-


      linksFullAC.addItem({linkid:?, label:userenteredlabel.text})


      Except I want the link id value to autoincrement so that each time they add an item it is the value of the highest linkid in the array collection +1. For example if the highest linkid in the array collection is 53 and the user clicks this button the new link would be assigned a value of 54 for the linkid. Can anyone please help me out on the code I'd use to do this?

        • 1. Re: How to find the Value of a Property in the Last Object of An Array Collection
          David_F57 Level 5




          var tmpid:Number = linksFullAC[linksFullAC.length-1].linkid+1;


          tmpid is your new ID.



          1 person found this helpful
          • 2. Re: How to find the Value of a Property in the Last Object of An Array Collection
            Sebastien V. Level 3

            I might be wrong, but for what i understood the maximum value of the linkid in the ArrayCollection will not necessarly be for the object situated at the last position of the ArrayCollection.


            Try this:


            private function getMaxLinkidValue( ac:ArrayCollection ):int {
                 if ( ac == null || ac.length == 0 )
                      return 0;
                 var max:int = ac.getItemAt( 0 ).linkid;
                 for each ( var obj:Object in ac )
                      if ( obj.linkid > max )
                           max = obj.linkid;
                 return max;


            linksFullAC.addItem({linkid:getMaxLinkidValue( linksFullAC ), label:userenteredlabel.text})
            1 person found this helpful
            • 3. Re: How to find the Value of a Property in the Last Object of An Array Collection
              jimmyoneshot Level 1

              Both those answers are good. Thanks. Upon looking again there is a problem. It's complicated so please bear with me. Upon application startup my array collection is produced dynamically via a http request to a mysql database. Then as I say the user can add items to the AC. The thing is when the user adds an item it also adds the same item into the mysql database. The user can also remove items which also removes the same items from my database based on the linkid value. The thing is this, the linkid's in my array collection and the link id's in my database table need to match and if you've used mysql before you'll know that you can set the id value of each table to autoincrement, which is a good strategy to ensure each item is unique when it's added to the table. However if the mysql database table linkid column is up to number 45 and the user deletes the last 5 values so it's up to 40 and then adds a new item the item added will be number 46 so it will skip the others that have been deleted whereas in flex if I did the same using that code it would simply re-add number 41. I know it's confusing


              Am I right in thinking that I'm going to have to implement some new php code which gets the highest value of link d from my mysql table and work with that as my value?

              • 4. Re: How to find the Value of a Property in the Last Object of An Array Collection
                David_F57 Level 5



                When the new item is added to the array then any value that is passed to the database will be ignored if it is an autoincrementing field, that makes it a lot easier as when the new item is updated you have your new itemid. So really if the collection is being syncronised with the database you shouldn't need to worry about calculating a new id.


                Or if you are manually maintaining the array , add the values to the db then use the result event to update the array.



                • 5. Re: How to find the Value of a Property in the Last Object of An Array Collection
                  jimmyoneshot Level 1

                  Yep that is definitely right and was sort of what I had in the past but you see I altered my method to speed up the application for the user because to get a result event I'd have to resend the request to the server and wait for the result to come back which would mean my array collection would have to refresh. What I forgot to mention is my array collection populates a tilelist and each item has an image property which is the name of the file on the server so what the user sees is a tilelist of images. A refresh takes a lot of time because of all these images. So what I decided to do was handle the data within flex i.e. get all the data via http request as soon as the app starts and filter it within flex via a filter function applied to my array collection. The user has the option to add, remove or update items, this all still happens in the background as the user activates these functions but I've removed the refresh (request to server and result event each time the user makes a change) of the data to speed up what the user sees by emulating these actions within the app itself i.e. if the user selects an item and updates it's details it gets updated on the server but I also use the setitemat method to update that link's object within the app, if a user removes the item then the item they've selected gets removed from the database but I also use the removeitemat method to remove the item object that is displayed within the app. This speeds up the process for the user whilst the live data is getting updated/removed on the server without slowing their experience down. I only want to get the majority of the data upon applicationcomplete and then allow the user to handle such data via flex with requests going on in the background not affecting the speed for them. The problem is though selecting a specific item and doing an interaction gets the linkid from that item and sends it to the server via php to either remove it or update it based on that specific value but with the additem function there is no existing linkid which is my problem and I don't want to overwrite others/have one that doesn't match the corresponding one on the server. Sorry for the long explanation but it's the only way I could describe it.


                  It seems my best and only bet is to retrieve the highest linkid value from the database itself via php as soon as the app starts then I can keep sending requests for this value each time the user updates/removes/adds that way it'll still be fast as it won't be retrieving all the data from the server but will allow me to get a correct and unique linkid for a new item.

                  • 6. Re: How to find the Value of a Property in the Last Object of An Array Collection
                    David_F57 Level 5



                    Another way would be to have a php service routine that appends a blank record to the database returning the new id, place the id into the new array record and then do an update, this should be pretty quick as you have nothing going up to the database when retrieving the id and only the id coming back.


                    With images in a few of my apps I actually stored the image into the array (reduced in size to suit my image list), This is pretty kewl as a jpg bytearray for say a 200x150 image is only about 10k so a thousand images use about 10-12 mbytes of local memory, if you use png for the resize its about 30 mb of local memory (most thumbs are usually more like 100x75).  If I needed the full size image I simply downloaded it but for general list navigation I was totally independent from the server.




                    [edit] for super fast you can create the thumb nail on the server.