Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Working outside of current context - with props and includes

Hi there,

Having spent a couple of days refactoring my app to reduce the frequency of API calls (and 'waits' between screens) I thought'd I'd share my findings on how I managed to update data 'across' contexts (and keep the UX flowing):

I have 4 tabs each with a window housing a tableview. each tableview is sourced from 1 or more API calls. I needed a way to update them in one go. This is the approach I took and it seems to work well.

  1. I created a data_access.js to hold all the Call functions, each calls a single API and puts the results in a list property. I 'include' this in the javascript file for each window (In order to optimise 'wait times' these calls go out asynchronously).

  2. Since we don't know what order the responses will arrive, there is a 'dataReadyCheck' function in every window which checks each call (using 'ready' flags). Only once all flags say 'ready' does it proceed to display the tableview.

  3. So whatever window the user is on, all lists get updated. When the user changes tabs (window focus) it checks for data in the List property and if empty fires the update again - just in case there was some unforeseen issue. otherwise it immediately pulls the latest data from the List property and updates the tableview - bingo!

  4. Since I'm using an API with a usage limit and I'm making multiple calls I make sure the refresh button is disabled until the process is complete. If the user gets impatient and multi-taps 'refresh' they get locked out from the API for 20 seconds - not good!).

One final point, each window js MUST use the same function name for 'dataReadyCheck' because the Data_Access functions all return to this in the window JS.

Note: If the number of calls required ever exceeds the API limit I would apply a timer to fire the calls in a 'ripple' so as to keep within the limit.

If you have a need to work across contexts - using a common include file and properties (as a 'global back-pocket') might be a useful option.

Hope this helps.

cheers,
Chris.

— asked March 23rd 2010 by Chris Reed
  • contexts
  • iphone
  • javascript
1 Comment
  • Hi Chris,
    Can you please provide the code sample for this???
    It will be very useful to understand also…
    Thanks!

    — commented September 14th 2010 by Sandil Shah

4 Answers

  • Very helpful, thanks!

    — answered March 23rd 2010 by Regis Zaleman
    permalink
    0 Comments
  • I'd be very interested to see how you are checking that all the flags are set before updating the table view as i'm having issues with my app.

    I run several requests asynchronously as this speeds up wait time, but i'm having issues determining when all the requests have completed.

    How are you achieving this?

    — answered April 6th 2010 by Lee Smith
    permalink
    0 Comments
  • Sure thing Lee

    First I set an array, with one attribute for each call, you could have seperate variables but to me it's a bit neater (less likely to forget one later!).

    var apiData = {companies: null, people: null, projects: null};
    

    Then at the end of each parse function I set the relevant attribute to "ready" and go to a check function (dataReadyCheck), for example:

    ...
    Titanium.App.Properties.setList ("peopleList",peopleArray);
    apiData.people = "ready";
    dataReadyCheck();
    

    In the 'check' function I see whether all values say "ready", so this function is called after each call, but only at the final time will it pass through. At that point the code goes to the function which uses the received data, which I now know has all arrived OK (here called displayData();

    function dataReadyCheck(){
        if (apiData.people == "ready" && apiData.companies == "ready" && apiData.projects == "ready") 
        {    
            displayData();
        };
    };
    

    I don't pass large array variables around, because I found that a bit flaky once you have 3 or more, The parser functions save to lists and displayData gets from lists. This also makes the code a bit more modular and easier to reuse.

    Hope that helps with a bit of detail.

    cheers,
    Chris.

    — answered April 6th 2010 by Chris Reed
    permalink
    0 Comments
  • Cheers chris, this is exactly what I needed! thanks for the tips :)

    — answered April 7th 2010 by Lee Smith
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.