Titanium Community Questions & Answer Archive

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

Execution context of app.js

I had a specific question about the execution context of app.js. If I have a event loop (while True) running in app.js and I move between different native controls does the loop continue to run or is it paused? Will code in app.js run across all controls?

— asked November 23rd 2010 by Kashif Razzaqui
  • mobile
0 Comments

2 Answers

  • Hi Kashif

    Thanks for an interesting question, which provoked an interesting subsequent discussion! :)

    In Titanium the code of a single context runs in a single thread with each line of code run in sequential order. Whereas, a new context (invoked, for example, by a new window's creation) runs in a separate and new thread.

    If a loop exists and, inside that loop, another context is invoked, the child context will not block the loop's execution, meaning that both may run simultaneously.

    > If I have a event loop (while True) running in app.js and I move between different native controls does the loop continue to run or is it paused?

    On this point, you seem to imply that you intend for the loop to run continuously, waiting on a condition to be satisfied before taking action. Don Thorp, from the core dev team, believes this would be bad practice, as it would literally "suck the battery right out of the device". This would likely lead to you losing a significant amount of your user base due to its bad effects.

    The recommendation is for you to instead use custom events, which can work across contexts. Alternatively, if you are interested in benchmarking and optimising your app, there is a technique using Timers that allows you to emulate asynchronous processing that can help to improve the responsiveness of an app in certain situations. Don Thorp has verified that this would be possible.

    Cheers

    Credit/appreciation to Don for his patience in explaining this, and correcting my initial understanding! :)

    — answered November 23rd 2010 by Paul Dowsett
    permalink
    0 Comments
  • Thanks for updating the answer. Wasn't actually going to use an infinite loop, just wanted to understand some behaviour

    Cheers
    Kashif

    — answered November 26th 2010 by Kashif Razzaqui
    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.