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?
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! :)
-
Thanks for updating the answer. Wasn't actually going to use an infinite loop, just wanted to understand some behaviour
Cheers
Kashif