Hi Madan

Caching could be very simple in your case, just keep it in some 
(optimized) datastructure in your class. It could be simple 
ArrayCollection or HashMap also.

I would prefer Singleton over static for many reasons (extensibility is 
one). So you can create one Singleton class, which basically does the 
work of looking up for cities. This class can hold the logic of return 
the cached cities, if it's already available.

For example, following might go in that class. Note, just rougly put up 
things, this can be much better (in terms of OO and algo):



private var citiesCache = new Dictonary (true);

function getCities (state:String):void
{
       if (getCitiesFromCache (state) is Array)
     {

       //assuming, you have created a custom event for State change - I 
just named it StateInformationEvent - though it's confusing, if you 
think Flex states :-)
       dispatchEvent (new StateInformationEvent (state, 
getCitiesFromCache (state));  
    }
    else
    {

          loadCitiesData (state);
    }
       
}


private function loadCitiesData (state:String):void
{
      //fetch cities from server
          var request  = new URLRequest 
("http://foo.com/getCities.php?state="; + state);
          var urlLoader:URLLoader = new URLLoader ();
          urlLoader.addEventListener (Event.COMPLETE, getCitiesHandler);
          urlLoader.load (request);

          //create inner function, it's much cleaner IMO - in some scenarios
           private function getCitiesHandler (event:Event):void
        {
                var cities:Array;
                               / /parse the output, instantiate cities 
Array and populate it.
                         //TODO
         
                   cacheCities (state, cities);

                dispatchEvent (new StateInformationEvent (state, cities));
        }
}


private function getCitiesFromCache (state:String):Array
{

     //Check if cities for this state is already stored. For the sake of 
brevity, left the exception
    //handling here, it might through exception if array is undefined. 
Also optimizations like, creating constant for citiesInState [state]
    // and using it everywhere in the block, instead of repeated calls.
    if (citiesCache [state] is Array && citiesCache[state].length > 0)
    {
       return citiesCache [state];
     }
      return null;
}


private function cacheCities (state:String, cities:Array):void
{
    citiesCache[state] = cities;
}



      

Madan Narra wrote:
> Hi Abdul,
>
> Thanks for the response...
>
> I am not much sure about caching stuff, as i haven't done any work 
> over that...
>
> Can you please elaborate how can i cache the cities as u explained...
>
> The States and Cities are List of objects that i receive from the 
> remote calls.
>
> States.class holds the following fields
> 1. ID
> 2. Name
>
> The same way the Cities.class
> 1. ID
> 2. stateID
> 3. Name
>
> So if at all i get the Cities and place that in memory, there would be 
> a total of *55k+ Cities object in memory*.
>
> I didn't understand what u were saying at point no 3:
> /3) You can have data in model - which could be just one instance and can
> be accessed application wide./
>
> If at all i have a static class with a static variable holding all the 
> cities objects, this can be used all over the application rt ? Please 
> right me if am wrong....
>
> Thanks,
> Madan N
>
> On Mon, Sep 8, 2008 at 11:13 AM, Abdul Qabiz <[EMAIL PROTECTED] 
> <mailto:[EMAIL PROTECTED]>> wrote:
>
>
>     Hi Madan
>
>     First, loading all data initially is No No. You can load when user
>     selects the state, then cache the cities-data in memor or shared
>     object.
>     I don't think, 55K strings would be too huge to keep in memory, that's
>     the worst case - when user goes through all the states, which
>     would not
>     happen very often, unless you hit an user who intentionally does
>     it :-)
>
>     You can optimize things further, you can have a tolerance or delay in
>     combo-selection, which means if user presses arrow key - selection
>     would
>     change too quickly - firing a request for each change event would lead
>     to unnecessary calls (http or whatever). You can have a delay in
>     there,
>     if change event is triggered too many times within a second, you might
>     make call once and that is for the last selected item.
>
>     1) Try to cache cities, as and when you receive data first time,
>     reuse it
>     2) Optimize the selection stuff - by making it more smart
>     3) You can have data in model - which could be just one instance
>     and can
>     be accessed application wide.
>     4) 55k strings (assuming cities) should not be problem keeping memory,
>     hopefully that would never happen, however it's worst case scenario.
>
>
>     -abdul
>
>     Madan Narra wrote:
>     > Hi All,
>     >
>     > This is not in specific with Flex, but more general with
>     applications.
>     >
>     > I am trying to put up a registration page, where in the user
>     needs to
>     > select his/her state and Cities.
>     >
>     > There are around 65 States and each state having there corresponding
>     > cities which totals the count to 55K+.
>     >
>     > I am populating these data in Combo box and showing to the user. The
>     > first time the user selects the State, and based upon his selection
>     > cities under this state get populated.
>     >
>     > These components are being used frequently in all over the
>     application.
>     >
>     > Now my question is, Is it fesiable to write a static class
>     basically a
>     > singleton which loads all the states and cities in the initial load,
>     > and use them where ever necessary ? or go for individual remote call
>     > in each page to fetch the details ?
>     >
>     > As states are of lesser number we can handle them to be placed in
>     > memory... But what with 55K cities..basically all these are
>     objects..
>     >
>     > Is it fesiable to bet with the memory to hold all these objects
>     in the
>     > memory throughout the application ?
>     >
>     > Please suggest or help me out with a solution which bests suite the
>     > above problem..
>     >
>     > Thanks a lot..
>     >
>     > Madan N
>     >
>     >
>     >
>     > >
>
>
>
>
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Flex 
India Community" group.
To post to this group, send email to flex_india@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/flex_india?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to